#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 11;
int n, k, match[26], par[maxn][26];
string s, a, b;
bool gone[26];
bool solve() {
  int i = 0;
  for (; i < n; i++) {
    if (match[s[i] - 'a'] != -1 and match[s[i] - 'a'] != a[i] or
        match[s[i] - 'a'] == -1 and gone[a[i] - 'a']) {
      i--;
      break;
    }
    match[s[i] - 'a'] = a[i];
    gone[a[i] - 'a'] = true;
  }
  if (i == n) return true;
  for (; i >= -1; i--) {
    int j = i + 1;
    if (match[s[j] - 'a'] < 0) {
      for (char x = a[j] + 1; x < 'a' + k; x++) {
        if (!gone[x - 'a']) {
          match[s[j] - 'a'] = x;
          gone[x - 'a'] = true;
          return true;
        }
      }
    } else if (match[s[j] - 'a'] > a[j])
      return true;
    if (i >= 0 and par[i][s[i] - 'a'] == 1) {
      gone[match[s[i] - 'a'] - 'a'] = 0;
      match[s[i] - 'a'] = -1;
    }
  }
  return false;
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    memset(match, -1, sizeof match);
    memset(gone, 0, sizeof gone);
    memset(par[0], 0, sizeof par[0]);
    cin >> k >> s >> a >> b;
    n = (int)(s).size();
    par[0][s[0] - 'a']++;
    for (int i = 1; i < n; i++) {
      memcpy(par[i], par[i - 1], sizeof par[i - 1]);
      par[i][s[i] - 'a']++;
    }
    if (solve()) {
      string ans = "";
      for (int i = 0; i < n; i++) {
        if (match[s[i] - 'a'] == -1) {
          int j = 0;
          while (gone[j]) j++;
          assert(j < k);
          gone[j] = true;
          match[s[i] - 'a'] = 'a' + j;
        }
        ans.push_back((char)match[s[i] - 'a']);
      }
      for (int i = 0; i < k; i++) {
        if (match[i] == -1) {
          int j = 0;
          while (gone[j]) j++;
          assert(j < k);
          match[i] = 'a' + j;
          gone[j] = true;
        }
      }
      if (ans <= b) {
        assert(ans >= a);
        cout << "YES\n";
        for (int i = 0; i < k; i++) cout << (char)match[i];
        cout << '\n';
      } else {
        cout << "NO\n";
      }
    } else
      cout << "NO\n";
  }
  return 0;
}
