#include <bits/stdc++.h>

using namespace std;

const int N = 30;

int w1[N], l1[N], r1[N], idx1=1, root1;
int w2[N], l2[N], r2[N], idx2=1, root2;
string pre1;
string pre2;
string in1;
string in2;

void insert1(int &u, int x){
	if (!u){
		u = idx1 ++;
		w1[u] = x;
	}
	else if (x < w1[u]){
		insert1(l1[u], x);
	}
	else if (x > w1[u]){
		insert1(r1[u], x);
	}
}

void insert2(int &u, int x){
	if (!u){
		u = idx2 ++;
		w2[u] = x;
	}
	else if (x < w2[u]){
		insert2(l2[u], x);
	}
	else if (x > w2[u]){
		insert2(r2[u], x);
	}
}

void pre_order1(int u){
	if (!u) return;
//	cout << w1[u];
	pre1.push_back(char(w1[u]));
	pre_order1(l1[u]);
	pre_order1(r1[u]);
}

void in_order1(int u){
	if (!u) return;
	
	in_order1(l1[u]);
//	cout << w1[u];
	in1.push_back(char(w1[u]));
	in_order1(r1[u]);
}

void pre_order2(int u){
	if (!u) return;
//	cout << w2[u];
	pre2.push_back(char(w2[u]));
	pre_order2(l2[u]);
	pre_order2(r2[u]);
}
void in_order2(int u){
	if (!u) return;
	
	in_order2(l2[u]);
//	cout << w2[u];
	in2.push_back(char(w2[u]));
	in_order2(r2[u]);
}


int main(){
	int n;
	while (cin >> n){
		if (n == 0) break;
		string a;
		cin >> a;
		idx1 = 1;
		root1 = 0;
		for (int i=0; i<N; i++){
			w1[i] = l1[i] = r1[i] = 0;
		}
		for (auto item:a){
			insert1(root1, item -'0');
		} 
		pre1.clear();
		in1.clear();
		pre_order1(root1);
//		puts("");
		in_order1(root1);
//		puts("");
		while (n --){
			idx2 = 1;
			root2 = 0;
			for (int i=0; i<N; i++){
				w2[i] = l2[i] = r2[i] = 0;
			}
			string b;
			cin >> b;
			
			for (auto item:b){
				insert2(root2, item-'0');
			}
			pre2.clear();
			in2.clear();
			pre_order2(root2);
//			puts("");
			in_order2(root2);
//			puts("");
			
			if (pre1 == pre2){
				puts("YES");
			}
			else{
				puts("NO");
			}
			
		}
	} 
}