#include <bits/stdc++.h>
using namespace std;
const double PI = atan2(1, 0) * 2;
const string DIGITS = "0123456789";
const string ALPH = "abcdefghijklmnopqrstuvwxyz";
template <class T>
ostream &operator<<(ostream &out, vector<T> &a) {
  out << "{";
  for (auto x : a) out << x << " ";
  return out << "}";
}
void smain();
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(12) << fixed;
  smain();
}
long long k;
string cur, low, up;
bool check_l(map<char, char> tr, char c, char c1, string cur, string low) {
  set<char> fre;
  for (long long i = 0; i < k; ++i) fre.insert('a' + i);
  tr[c] = c1;
  for (auto p : tr) fre.erase(p.second);
  for (long long i = 0; i < ((long long)(cur.size())); ++i) {
    if (!tr.count(cur[i])) {
      tr[cur[i]] = *fre.rbegin();
      fre.erase(*fre.rbegin());
    }
    cur[i] = tr[cur[i]];
  }
  for (long long i = 0; i < k; ++i)
    if (!tr.count('a' + i)) {
      tr['a' + i] = *fre.begin();
      fre.erase(fre.begin());
    }
  if (cur >= low && cur <= up && fre.empty()) {
    cout << "YES\n";
    for (long long i = 0; i < k; ++i) cout << tr['a' + i];
    cout << '\n';
    return true;
  } else {
    return false;
  }
}
bool check_u(map<char, char> tr, char c, char c1, string cur, string up) {
  set<char> fre;
  for (long long i = 0; i < k; ++i) fre.insert('a' + i);
  tr[c] = c1;
  for (auto p : tr) fre.erase(p.second);
  for (long long i = 0; i < ((long long)(cur.size())); ++i) {
    if (!tr.count(cur[i])) {
      tr[cur[i]] = *fre.begin();
      fre.erase(fre.begin());
    }
    cur[i] = tr[cur[i]];
  }
  for (long long i = 0; i < k; ++i)
    if (!tr.count('a' + i)) {
      tr['a' + i] = *fre.begin();
      fre.erase(fre.begin());
    }
  if (cur >= low && cur <= up && fre.empty()) {
    cout << "YES\n";
    for (long long i = 0; i < k; ++i) cout << tr['a' + i];
    cout << '\n';
    return true;
  } else {
    return false;
  }
}
void check(map<char, char> tr, string cur) {
  set<char> fre;
  for (long long i = 0; i < k; ++i) fre.insert('a' + i);
  for (auto p : tr) fre.erase(p.second);
  for (long long i = 0; i < ((long long)(cur.size())); ++i) {
    if (!tr.count(cur[i])) {
      tr[cur[i]] = *fre.begin();
      fre.erase(fre.begin());
    }
    cur[i] = tr[cur[i]];
  }
  for (long long i = 0; i < k; ++i)
    if (!tr.count('a' + i)) {
      tr['a' + i] = *fre.begin();
      fre.erase(fre.begin());
    }
  if (cur >= low && cur <= up && fre.empty()) {
    cout << "YES\n";
    for (long long i = 0; i < k; ++i) cout << tr['a' + i];
    cout << '\n';
  } else {
    cout << "NO\n";
  }
}
void solution(long long k, string s, string a, string b) {
  map<char, char> trans;
  string cur;
  long long n = ((long long)(a.size()));
  for (long long i = 0; i < n; ++i) {
    if (a[i] == b[i]) {
      trans[s[i]] = a[i];
      continue;
    }
    if (trans.count(s[i])) {
      if (trans[s[i]] == a[i]) {
        if (check_l(trans, s[i], a[i], s, a))
          return;
        else {
          cout << "NO\n";
          return;
        }
      } else if (trans[s[i]] == b[i]) {
        if (check_u(trans, s[i], b[i], s, b))
          return;
        else {
          cout << "NO\n";
          return;
        }
      } else if (trans[s[i]] < a[i] || trans[s[i]] > b[i]) {
        cout << "NO\n";
        return;
      } else {
        check(trans, s);
        return;
      }
    }
    if (a[i] + 1 == b[i]) {
      if (check_l(trans, s[i], a[i], s, a)) return;
      if (check_u(trans, s[i], b[i], s, b)) return;
      cout << "NO\n";
      return;
    } else {
      set<char> fre;
      for (long long c = a[i] + 1; c < b[i]; ++c) fre.insert(c);
      for (auto p : trans) fre.erase(p.second);
      if (((long long)(fre.size()))) {
        trans[s[i]] = *fre.begin();
        check(trans, s);
        return;
      } else {
        if (check_l(trans, s[i], a[i], s, a)) return;
        if (check_u(trans, s[i], b[i], s, b)) return;
        cout << "NO\n";
        return;
      }
    }
  }
  check(trans, s);
}
void smain() {
  long long t;
  cin >> t;
  while (t--) {
    cin >> k >> cur >> low >> up;
    solution(k, cur, low, up);
  }
}
