rt,fst了/kk
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5 + 5;
int t, a[N];
vector<int> v1[N], v2[200];
map<int, int> mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> t;
while(t--) {
int n, cnt = 0; cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
if(!mp[a[i]]) mp[a[i]] = ++cnt;
v1[mp[a[i]]].push_back(i);
}
int m; cin >> m;
while(m--) {
string s; cin >> s;
int len = s.size(), f = 0;
s = " " + s;
if(len != n) {
f = 1;
}
for(int i = 1; i <= len; i++) {
v2[s[i]].push_back(i);
// cout << s[i] << " " << i << endl;
}
for(int i = 1; i <= cnt; i++) {
for(int j = 1; j < v1[i].size(); j++) {
if(s[v1[i][j]] != s[v1[i][j-1]]) {
f = 1;
// cout << s[v1[i][j]] << " " << s[v1[i][j-1]] << " 1"<< endl;
break;
}
}
}
for(int i = 1; i < 200; i++)
for(int j = 1; j < v2[i].size(); j++) {
if(a[v2[i][j]] != a[v2[i][j-1]]) {
f = 1;
// cout << v2[i][j] << " " << v2[i][j-1] << " 2"<<endl;
// cout << a[v2[i][j]] << " " << a[v2[i][j-1]] << " 2"<<endl;
break;
}
}
if(f == 1) puts("NO");
else puts("YES");
// puts
for(int i = 1; i < 200; i++) v2[i].clear();
}
for(int i = 1; i <= cnt; i++) v1[i].clear();
mp.clear();
}
return 0;
}