#include <bits/stdc++.h>
using namespace std;
void OPEN(string s)
{
    freopen((s + ".in").c_str(),"r", stdin);
    freopen((s + ".out").c_str(),"w", stdout);
}
string s[4], tr;
map<string,bool> mp;
void solve(int idx)
{
    string tm = tr;
    sort(tm.begin(), tm.end());
    mp[tm] = 1;
    if(idx >= 4)
        return;
    for(int i = 0; i < 6; ++i)
    {
        tr += s[idx][i];
        solve(idx + 1);
        tr.pop_back();
    }
    solve(idx + 1);
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    for(int i = 0; i < 4; ++i)
        cin >> s[i];
    tr = "";
    solve(0);
    while(n--)
    {
        string x;
        cin >> x;
        sort(x.begin(),x.end());
        if(mp[x])
            cout << "YES\n";
        else
            cout << "NO\n";
    }
}