#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000005;
int t;
int k;
int len;
char s[maxn];
char a[maxn];
char b[maxn];
char temp[30];
char dps[maxn];
bool found = false;
int vis[30];
bool cmp(char* m, char* n) {
  for (int i = 0; i < len; i++) {
    if (m[i] > n[i])
      return false;
    else if (m[i] < n[i])
      return true;
  }
  return true;
}
void printy() {
  found = true;
  puts("YES");
  vector<int> left;
  int cnt = 0;
  for (int i = 0; i < k; i++)
    if (!vis[i]) left.push_back(i);
  for (int i = 0; i < k; i++)
    if (temp[i] == 0) temp[i] = left[cnt++] + 'a';
  printf("%s\n", temp);
}
bool check(int d) {
  if (d == len) {
    if (cmp(dps, b)) {
      printy();
      return true;
    }
    return false;
  }
  char cur = temp[s[d] - 'a'];
  if (cur != 0) {
    dps[d] = cur;
    if (check(d + 1))
      return true;
    else
      return false;
  } else {
    char tmp = 0;
    while (vis[tmp] && tmp < k) tmp++;
    if (tmp == k)
      return false;
    else {
      dps[d] = tmp + 'a';
      temp[s[d] - 'a'] = tmp + 'a';
      vis[tmp] = 1;
      bool res = check(d + 1);
      temp[s[d] - 'a'] = 0;
      vis[tmp] = 0;
      return res;
    }
  }
}
void dp(int d) {
  if (found) return;
  if (d == len) {
    if (cmp(dps, b)) {
      printy();
      found = true;
    }
    return;
  }
  char cur = temp[s[d] - 'a'];
  if (cur != 0) {
    if (cur < a[d]) return;
    dps[d] = temp[s[d] - 'a'];
    if (dps[d] > a[d]) {
      if (check(d + 1)) found = true;
      return;
    }
    dp(d + 1);
  } else {
    if (!vis[a[d] - 'a']) {
      dps[d] = a[d];
      temp[s[d] - 'a'] = a[d];
      vis[a[d] - 'a'] = 1;
      dp(d + 1);
      vis[a[d] - 'a'] = 0;
      temp[s[d] - 'a'] = 0;
    }
    if (!found) {
      for (char i = a[d] + 1; i < 'a' + k; i++)
        if (!vis[i - 'a']) {
          dps[d] = i;
          temp[s[d] - 'a'] = i;
          vis[i - 'a'] = 1;
          if (check(d + 1)) {
            found = true;
            return;
          }
          temp[s[d] - 'a'] = 0;
          vis[i - 'a'] = 0;
        }
    }
  }
}
int main() {
  scanf("%d ", &t);
  while (t--) {
    scanf("%d", &k);
    scanf("%s", s);
    scanf("%s", a);
    scanf("%s", b);
    len = strlen(s);
    memset(temp, 0, sizeof temp);
    memset(vis, 0, sizeof vis);
    found = false;
    dp(0);
    if (!found) cout << "NO" << endl;
  }
}
