#include <bits/stdc++.h>
using namespace std;
template <typename T>
using minpq = priority_queue<T, vector<T>, greater<T>>;
const int K = 27, N = 1e6 + 5;
int t, k, n, p[K];
bool used[N];
string s, a, b;
bool solve(int i, bool r) {
  if (i == n) return true;
  if (!r) {
    if (p[s[i] - 'a'] == -1) {
      for (int c = (0); c < (k); c++) {
        if (!used[c]) {
          p[s[i] - 'a'] = c;
          used[c] = true;
          break;
        }
      }
    }
    return solve(i + 1, false);
  }
  if (p[s[i] - 'a'] != -1) {
    if (p[s[i] - 'a'] < a[i] - 'a') {
      return false;
    } else if (p[s[i] - 'a'] == a[i] - 'a') {
      return solve(i + 1, true);
    } else {
      return solve(i + 1, false);
    }
  } else {
    if (!used[a[i] - 'a']) {
      p[s[i] - 'a'] = a[i] - 'a';
      used[a[i] - 'a'] = true;
      if (solve(i + 1, true)) return true;
      p[s[i] - 'a'] = -1;
      used[a[i] - 'a'] = false;
    }
    for (int c = (a[i] - 'a' + 1); c < (k); c++) {
      if (!used[c]) {
        p[s[i] - 'a'] = c;
        used[c] = true;
        return solve(i + 1, false);
      }
    }
    return false;
  }
}
void printans() {
  int ch = 0;
  for (int i = (0); i < (k); i++) {
    if (p[i] == -1) {
      while (used[ch]) ch++;
      assert(ch < k);
      used[ch] = true;
      p[i] = ch;
    }
    cout << (char)('a' + p[i]);
  }
  cout << '\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> t;
  while (t--) {
    cin >> k >> s >> a >> b;
    n = s.length();
    fill(p, p + k, -1);
    fill(used, used + k, false);
    if (!solve(0, true)) {
      cout << "NO\n";
      continue;
    }
    for (int i = (0); i < (n); i++) {
      s[i] = 'a' + p[s[i] - 'a'];
    }
    if (a <= s && s <= b) {
      cout << "YES\n";
      printans();
    } else {
      cout << "NO\n";
    }
  }
}
