#include <bits/stdc++.h>
#define maxn 100010
#define inf 0x3f3f3f3f
#define REP(i,x,y) for(int i=x;i<(y);i++)
#define RREP(i,x,y) for(int i=x;i>(y);i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
string s,good;
int ma[30];
int main()
{
    //ios::sync_with_stdio(false);
    cin>>good;
    REP(i,0,good.length())
        ma[good[i]-'a']=1;
    cin>>s;
    int pos=-1;
    REP(i,0,s.length())
        if(s[i]=='*') {pos=i;break;}
    int suf=0,pre=0;
    REP(i,0,s.length()){
        if(i<pos) pre++;
        else if(i>pos) suf++;
    }
    int q;
    scanf("%d",&q);
    while(q--){
        string t;cin>>t;int flag=1;
        if(pos==-1&&t.length()>s.length()) {puts("NO");continue;}
        int sub=t.length()-suf-pre;
        int j=0;
        REP(i,0,s.length()){
            if(s[i]==t[j]) j++;
            else if(s[i]=='?'){
                if(ma[t[j]-'a']) j++;
                else {flag=0;break;}
            }
            else if(s[i]=='*'){
                if(sub==0) continue;
                else if(sub>0){
                    int ed=j+sub;
                    while(j<ed){
                        if(ma[t[j]-'a']==0&&((t[j]>='a'&&t[j]<='z')||t[j]==' ')) j++;
                        else {flag=0;break;}
                    }
                    if(flag==0) break;
                }
                else{
                    i+=(-sub);
                    i--;
                }
            }
            else {flag=0;break;}
        }
        if(flag) puts("YES");
        else puts("NO");
    }
}


#include <bits/stdc++.h>
#define REP(i,x,y) for(int i=x;i<(y);i++)

using namespace std;
typedef long long ll;
int ma[29];
int main()
{
    string good;
    cin >> good;
    for (int i = 0; i<good.length();i++){
        ma[good[i]-'a'] = 1;
    }
    string s;cin >> s;
    int num = 0;
    for (int i = 0; i<s.length();i++) if (s[i] == '*') num++;
    int q;cin >> q;
    while(q--)
    {
        string t;
        cin >> t;
        int flag = 1;
        if (t.length()==s.length())
        {
            for (int i = 0; i<t.length() ;i++)
            {
                if (s[i] == '?'&&ma[t[i]-'a']==0) flag = 0;
                if (s[i]!='?'&&s[i]!='*'&& s[i]!=t[i]) flag = 0;
                if (s[i]=='*' && ma[t[i]-'a']==1) flag = 0;
            }
            if(!flag)
                puts("NO");
            else puts("YES");
        }
        else if (num==1&&t.length()+1==s.length()){
            int j = 0;
            for (int i = 0;i<t.length(); i++)
            {
                if (s[j]=='?'&& ma[t[i]-'a']==0) flag = 0;
                if (s[j]=='*') {j++;}
                if (s[j]!='?'&&s[j]!='*' && s[j]!=t[i]) flag = 0;
                j++;
            }
            if(flag == 0)
                puts("NO");
            else puts("YES");
        }
        else if (t.length() > s.length() && num == 1)
        {
            int sub = t.length() - s.length();
            int j = 0;
            for (int i = 0;i<t.length(); i++)
            {
                if (s[j]=='*') {
                    for (int z = 1;z<=sub;i++,z++){
                        if (ma[t[i]-'a']==1) flag =0;
                        }
                    j++;
                }
                if (i+1 >= t.length()) break;
                if (s[j]!='?'&&s[j]!='*' && s[j]!=t[i]) flag = 0;
                if (s[j]=='?'&& ma[t[i]-'a']==0) flag = 0;
                j++;
            }
            if(flag == 0) puts("NO");
            else puts("YES");
        }
        else puts("NO");
    }
}


#include <bits/stdc++.h>
#define maxn 100010
#define inf 0x3f3f3f3f
#define REP(i,x,y) for(int i=x;i<(y);i++)
#define RREP(i,x,y) for(int i=x;i>(y);i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
string s,good;
map<char ,bool>ma;
int main()
{
    //ios::sync_with_stdio(false);
    cin>>good;
    REP(i,0,good.length())
        ma[good[i]]=1;
    cin>>s;
    int pos=0;
    REP(i,0,s.length())
        if(s[i]=='*') {pos=i;break;}
    int suf=0,pre=0;
    REP(i,0,s.length()){
        if(i<pos) pre++;
        else if(i>pos) suf++;
    }
    int q;
    scanf("%d",&q);
    while(q--){
        string t;cin>>t;int flag=1;
        int sub=t.length()-suf-pre;
        int j=0;
        REP(i,0,s.length()){
            if(s[i]==t[j]) j++;
            else if(s[i]=='?'){
                if(ma[t[j]]) j++;
                else {flag=0;break;}
            }
            else if(s[i]=='*'){
                if(sub==0) continue;
                else if(sub>0){
                    int ed=j+sub;
                    while(j<ed){
                        if(ma.count(t[j])==0&&((t[j]>='a'&&t[j]<='z')||t[j]==' ')) j++;
                        else {flag=0;break;}
                    }
                    if(flag==0) break;
                }
                else{
                    i+=(-sub);
                    i--;
                }
            }
            else {flag=0;break;}
        }
        if(flag) puts("YES");
        else puts("NO");
    }
}


#include <bits/stdc++.h>
#define REP(i,x,y) for(int i=x;i<(y);i++)

using namespace std;
typedef long long ll;
map<char,bool>ma;
int main()
{
    string good;
    cin >> good;
    for (int i = 0; i<good.length();i++){
        ma[good[i]] = 1;
    }
    string s;
    cin >> s;
    int num = 0;
    for (int i = 0; i<s.length();i++)
        if (s[i] == '*') num++;
    int q;
    cin >> q;
    while(q--)
    {
        string t;
        cin >> t;
        int flag = 1;
        if (t.length()==s.length())
        {
            for (int i = 0; i<t.length() ;i++)
            {
                if (s[i] == '?'&&ma[t[i]]==0) flag = 0;
                if (s[i]!='?'&&s[i]!='*'&& s[i]!=t[i]) flag = 0;
                if (s[i]=='*' && ma[t[i]]==1) flag = 0;
            }
            if(!flag)
                puts("NO");
            else puts("YES");
        }
        else if (num==1&&t.length()+1==s.length()){
            int j = 0;
            for (int i = 0;i<t.length(); i++)
            {
                if (s[j]=='?'&& ma[t[i]]==0) flag = 0;
                if (s[j]=='*') {j++;}
                if (s[j]!='?'&&s[j]!='*' && s[j]!=t[i]) flag = 0;
                j++;
            }
            if(flag == 0)
                puts("NO");
            else puts("YES");
        }
        else if (t.length() > s.length() && num == 1)
        {
            int sub = t.length() - s.length();
            int j = 0;
            for (int i = 0;i<t.length(); i++)
            {
                if (s[j]=='*') {
                    for (int z = 1;z<=sub;i++,z++){
                        if (ma[t[i]]==1) flag =0;
                        }
                    j++;
                }
                if (i+1 >= t.length()) break;
                if (s[j]=='?'&& ma[t[i]]==0) flag = 0;
                if (s[j]!='?'&&s[j]!='*' && s[j]!=t[i]) flag = 0;
                j++;
            }
            if(flag == 0)
                puts("NO");
            else puts("YES");
        }
        else
            puts("NO");
    }
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x=' ';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good,bad;
    for(x='a';x<='z';x++)bad[x]++;
    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++,bad[t[i]]--;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length(),com=0,inc=0;
        if(tl+1<p)cout<<"NO\n";
        else{
        for(j=0;j<p;j++){
            if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
            else if(pat[j]=='*'){
                if(tl<p)inc=-1;
                else{
                for(inc=0;inc<=tl-p;inc++){
                    if(!bad[t[j+inc]]){com=1;cout<<"NO\n";break;}
                }
                if(com)break;
                else inc--;
                }
            }
            else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
        }
        if(!com)cout<<"YES\n";
        }
    }
    return 0;
}

/*
a
aa*a
1
aa ba
*/


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;
    int i,j,q,p;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    for(i=0;i<t.length();i++)good[t[i]]++;

    for(i=0;i<q&&cin>>t;i++){
        if(t.length()>p||t.length()+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                if(pat[j]=='?'&&!good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com)cout<<"YES\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;
    int i,j,q,p,tl;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    for(i=0;i<t.length();i++)good[t[i]]++;

    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        if(tl>p||tl+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                if(pat[j]=='*')tl++;
                if(pat[j]=='?'&&!good[t[j]]){com=1;cout<<"NO\n";break;}
                //else if(pat[j]!='*'&& good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                if(tl<p)cout<<"NO\n";
                else cout<<"YES\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x='\0';
    int i,j,q,p,tl;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    good[x]++;
    for(x='a';x<='z';x++)good[x]++;
    for(x='A';x<='Z';x++)good[x]++;
    for(i=0;i<t.length();i++)good[t[i]]++;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        if(tl>p||tl+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&good[t[j]]==1){com=1;cout<<"NO\n";break;}
                //else if(pat[j]!='*'&& good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                if(tl<p)cout<<"NO\n";
                else cout<<"YES\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x;
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good;

    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        //if(tl>p )cout<<"NO\n";
        //else{
            com=0,inc=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'){
                    for(inc=0;inc<tl-p;inc++){
                        if(good[t[j+inc]]){com=1;cout<<"NO\n";break;break;}
                    }
                }
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        //}
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x=' ';
    int i,j,q,p,tl;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    good[x]++;
    for(x='a';x<='z';x++)good[x]++;
    for(x='A';x<='Z';x++)good[x]++;
    for(i=0;i<t.length();i++)x=t[i],good[x]++,x-=32,good[x]++;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        if(tl>p||tl+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&good[t[j]]==1){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'&&good[t[j]]==2){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x=' ';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good,bad;
    for(x='a';x<='z';x++)bad[x]++;
    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++,bad[t[i]]--;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length(),com=0,inc=0;
        if(tl+1<p)cout<<"NO\n";
        else{
        for(j=0;j<p;j++){
            if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
            else if(pat[j]=='*'){
                if(tl<p)inc=-1;
                else{
                for(inc=0;inc<=tl-p;inc++){
                    if(!bad[t[j+inc]]){com=1;cout<<"NO\n";break;}
                }
                if(com)break;
                else inc--;
                }
                tl=p;
            }
            else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
        }
        if(!com){
            if(tl!=p){com=1;cout<<"NO\n";}
            else cout<<"YES\n";
        }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x='\0';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good,bad;
    bad[x]++;
    for(x='a';x<='z';x++)bad[x]++;

    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++,bad[t[i]]--;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        //if(tl>p )cout<<"NO\n";
        //else{
            com=0,inc=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'){
                    for(inc=0;inc<tl-p;inc++){
                        if(!bad[t[j+inc]]){com=1;cout<<"NO\n";break;break;}
                    }
                }
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        //}
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x='\0';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good,bad;
    bad[x]++;
    for(x='a';x<'z';x++)bad[x]++;

    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++,bad[t[i]]--;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        //if(tl>p )cout<<"NO\n";
        //else{
            com=0,inc=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'){
                    for(inc=0;inc<tl-p;inc++){
                        if(!bad[t[j+inc]]){com=1;cout<<"NO\n";break;break;}
                    }
                }
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        //}
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x=' ';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    good[x]++;
    for(x='a';x<='z';x++)good[x]++;
    for(x='A';x<='Z';x++)good[x]++;
    for(i=0;i<t.length();i++)x=t[i],good[x]++,x-=32,good[x]++;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        //if(tl>p )cout<<"NO\n";
        //else{
            com=0,inc=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&good[t[j+inc]]==1){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'){
                    for(inc=0;inc<tl-p;inc++){
                        if(good[t[j+inc]]==2){com=1;cout<<"NO\n";break;break;}
                    }
                }
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        //}
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x=' ';
    int i,j,q,p,tl,inc;
    bool com;
    map<char,int> good,bad;
    for(x='a';x<='z';x++)bad[x]++;
    cin>>t>>pat>>q;
    p=pat.length();

    for(i=0;i<t.length();i++)good[t[i]]++,bad[t[i]]--;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length(),com=0,inc=0;
        if(tl+1<p)cout<<"NO\n";
        else{
        for(j=0;j<p;j++){
            if(pat[j]=='?'&&!good[t[j+inc]]){com=1;cout<<"NO\n";break;}
            else if(pat[j]=='*'){
                if(tl<p)inc=-1;
                else{
                for(inc=0;inc<=tl-p;inc++){
                    if(!bad[t[j+inc]]){com=1;cout<<"NO\n";break;break;}
                }inc--;
                }
            }
            else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j+inc]){com=1;cout<<"NO\n";break;}
        }
        if(!com)cout<<"YES\n";
        }
    }
    return 0;
}

/*
a
aa*a
1
aa ba
*/


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;       char x;
    int i,j,q,p,tl;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    //good[x]++;
    //for(x='a';x<='z';x++)good[x]++;
    //for(x='A';x<='Z';x++)good[x]++;
    for(i=0;i<t.length();i++)x=t[i],good[x]++,x-=32,good[x]++;
    for(i=0;i<q&&cin>>t;i++){
        tl=t.length();
        if(tl>p||tl+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                //if(pat[j]=='*'){tl++;if(!good[t[j]]){com=1;cout<<"NO\n";break;}}
                if(pat[j]=='?'&&!good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]=='*'&&good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com){
                //if(tl<p)cout<<"NO\n";
                cout<<"YES\n";
            }
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string t,pat;
    int i,j,q,p;
    bool com;
    map<char,int> good;
    cin>>t>>pat>>q;
    p=pat.length();
    for(i=0;i<t.length();i++)good[t[i]]++;

    for(i=0;i<q&&cin>>t;i++){
        if(t.length()>p||t.length()+1<p)cout<<"NO\n";
        else{
            com=0;
            for(j=0;j<p;j++){
                if(pat[j]=='?'&&!good[t[j]]){com=1;cout<<"NO\n";break;}
                if(pat[j]!='*'&& good[t[j]]){com=1;cout<<"NO\n";break;}
                else if(pat[j]!='*'&&pat[j]!='?'&&pat[j]!=t[j]){com=1;cout<<"NO\n";break;}
            }
            if(!com)cout<<"YES\n";
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1,k=q.size()-1;j>=1;j--,k--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[k])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int f=0;f<q.size();f++)
                            {
                                if((int)w.find(q[f])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
             if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=0;k<q.size()-(s.size()-1);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int j=s.size()-1,d=q.size()-1;j>vec1[0];j--,d--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[d])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[d]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=vec1[0];k<q.size()-abs(vec1[0]+1-s.size());k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1,k=q.size()-1;j>=1;j--,k--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[k])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int f=0;f<q.size();f++)
                            {
                                if((int)w.find(q[f])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
             if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int j=s.size()-1,d=q.size()-1;j>vec1[0];j--,d--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[d])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[d]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<q.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int j=s.size()-1;j>vec1[0];j--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
int e=0;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool d=false,b=false;
	map<char,int>mp;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<w.size();i++){mp[w[i]]++;}
	if(s=="*"){d=true;}
	if((int)s.find("*")!=-1)
    {
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='*'){e=i;break;}
        }
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if(d)
        {
            for(int j=0;j<q.size();j++)
            {
                if(mp[q[j]]!=0){cout<<"NO"<<endl; b=true; break;}
            }
            if(b){b=false; continue;}
            cout<<"YES"<<endl; continue;
        }
        else
        {
            if((int)s.find("*")==-1)
            {
                if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
                for(int j=0;j<q.size();j++)
                {
                    if(s[j]=='?')
                    {
                        if(mp[q[j]]==0){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl; continue;
            }
            else
            {
                int j=0,k=0;
                if(s[0]=='*')
                {
                    k=0;j=0;
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=s.size()-1,k=q.size()-1;j>=1;j--,k--)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f>=0;f--)
                    {
                        if(mp[q[f]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else if(s[s.size()-1]=='*')
                {
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=0,k=0;j<s.size()-1;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f<q.size();f++)
                    {
                        if(mp[q[f]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else
                {
                    for(j=0,k=0;j<e;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    int f=0,r=0;
                    for(f=s.size()-1,r=q.size()-1;f>e;f--,r--)
                    {
                        if(s[f]=='?')
                        {
                            if(mp[q[r]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[f]!=q[r]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    if(q.size()<s.size()){cout<<"YES"<<endl; continue;}
                    for(int z=e;z<=r;z++)
                    {
                        if(mp[q[z]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                     if(b){b=false; continue;}
                     cout<<"YES"<<endl; continue;
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                for(int j=s.size()-1;j>vec1[0];j--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
             if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int j=s.size()-1;j>vec1[0];j--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){cout<<"YES"<<endl;continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                if(s.size()==1){cout<<"YES"<<endl; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int j=s.size()-1;j>vec1[0];j--)

                {
                     if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                if(q.size()==s.size()-1||q.size()==s.size()){continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
int e=0;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool d=false,b=false;
	map<char,int>mp;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<w.size();i++){mp[w[i]]++;}
	if(s=="*"){d=true;}
	if((int)s.find("*")!=-1)
    {
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='*'){e=i;break;}
        }
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if(d)
        {
            for(int j=0;j<q.size();j++)
            {
                if(mp[q[j]]!=0){cout<<"NO"<<endl; b=true; break;}
            }
            if(b){b=false; continue;}
            cout<<"YES"<<endl; continue;
        }
        else
        {
            if((int)s.find("*")==-1)
            {
                if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
                for(int j=0;j<q.size();j++)
                {
                    if(s[j]=='?')
                    {
                        if(mp[q[j]]==0){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl; continue;
            }
            else
            {
                int j=0,k=0;
                if(s[0]=='*')
                {
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=s.size()-1,k=q.size()-1;j>=1;j--,k--)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f>=0;f--)
                    {
                        if(mp[q[f]]==0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else if(s[s.size()-1]=='*')
                {
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=0,k=0;j<s.size()-1;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f<q.size();f++)
                    {
                        if(mp[q[f]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else
                {
                    for(j=0,k=0;j<e;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    int f=0,r=0;
                    for(f=s.size()-1,r=q.size()-1;f>e;f--,r--)
                    {
                        if(s[f]=='?')
                        {
                            if(mp[q[r]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[f]!=q[r]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    if(q.size()<s.size()){cout<<"YES"<<endl; continue;}
                    for(int z=e;z<=r;z++)
                    {
                        if(mp[q[z]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                     if(b){b=false; continue;}
                     cout<<"YES"<<endl; continue;
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
int e=0;
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool d=false,b=false;
	map<char,int>mp;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<w.size();i++){mp[w[i]]++;}
	if(s=="*"){d=true;}
	if((int)s.find("*")!=-1)
    {
        for(int i=0;i<s.size();i++)
        {
            if(s[i]=='*'){e=i;break;}
        }
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if(d)
        {
            for(int j=0;j<q.size();j++)
            {
                if(mp[q[j]]!=0){cout<<"NO"<<endl; b=true; break;}
            }
            if(b){b=false; continue;}
            cout<<"YES"<<endl; continue;
        }
        else
        {
            if((int)s.find("*")==-1)
            {
                if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
                for(int j=0;j<q.size();j++)
                {
                    if(s[j]=='?')
                    {
                        if(mp[q[j]]==0){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl; continue;
            }
            else
            {
                int j=0,k=0;
                if(s[0]=='*')
                {
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=s.size()-1,k=q.size()-1;j>=1;j--,k--)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f>=0;f--)
                    {
                        if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else if(s[s.size()-1]=='*')
                {
                    if(q.size()<s.size()){if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}}
                    for(j=0,k=0;j<s.size()-1;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    for(int f=k;f<q.size();f++)
                    {
                        if(mp[q[f]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                    if(b){b=false; continue;}
                    cout<<"YES"<<endl; continue;
                }
                else
                {
                    for(j=0,k=0;j<e;j++,k++)
                    {
                        if(s[j]=='?')
                        {
                            if(mp[q[k]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[j]!=q[k]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    int f=0,r=0;
                    for(f=s.size()-1,r=q.size()-1;f>e;f--,r--)
                    {
                        if(s[f]=='?')
                        {
                            if(mp[q[r]]==0){cout<<"NO"<<endl; b=true; break;}
                        }
                        else
                        {
                            if(s[f]!=q[r]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                    if(b){b=false; continue;}
                    if(q.size()<s.size()){cout<<"YES"<<endl; continue;}
                    for(int z=e;z<=r;z++)
                    {
                        if(mp[q[z]]!=0){cout<<"NO"<<endl; b=true; break;}
                    }
                     if(b){b=false; continue;}
                     cout<<"YES"<<endl; continue;
                }
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                for(int k=0;k<s.size()-1-q.size();k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                for(int k=q.size()-1;k>=s.size()-1;k--)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true;break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                for(int k=vec1[0];k<q.size()-(s.size()-1-vec1[0]);k++)
                {
                    if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                }
                if(!b)
                cout<<"YES"<<endl;
                else b=false;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
	for(int i=0;i<s.size();i++)
    {
        if(s[i]=='?'){vec.push_back(i);}
        if(s[i]=='*'){vec1[0]=i;}
    }
    int n; cin>>n;
    while(n--)
    {
        cin>>q;
        if((int)s.find("*")==-1)
        {
            if(q.size()!=s.size()){cout<<"NO"<<endl; continue;}
            for(int j=0;j<q.size();j++)
            {
                if(s[j]=='?')
                {
                   if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                }
                else
                {
                    if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                }
            }
            if(b){b=false;continue;}
            cout<<"YES"<<endl;
        }
        else
        {
            if(q.size()<s.size())
            {
                if(q.size()+1!=s.size()){cout<<"NO"<<endl; continue;}
            }
            if(s[0]=='*')
            {
                for(int j=s.size()-1;j>=1;j--)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl;b=true;break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                                if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
            else if(s[s.size()-1]=='*')
            {
                for(int j=0;j<s.size()-1;j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]=='*')
                        {
                            for(int k=0;k<q.size();k++)
                            {
                              if((int)w.find(q[k])!=-1){cout<<"NO"<<endl; b=true; break;}
                            }
                            if(b){break;}
                        }
                        else
                        {
                            if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                        }
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
            else
            {
                for(int j=0;j<vec1[0];j++)
                {
                    if(s[j]=='?')
                    {
                        if((int)w.find(q[j])==-1){cout<<"NO"<<endl; b=true; break;}
                    }
                    else
                    {
                        if(s[j]!=q[j]){cout<<"NO"<<endl; b=true; break;}
                    }
                }
                if(b){b=false; continue;}
                cout<<"YES"<<endl;
            }
        }
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
/*#include <sstream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <algorithm>
#include<climits>
#include <bitset>
#include <ctime>
#include <cassert>
#include <functional>
#include <iomanip>
#include <vector>
#include <stack>
#include <fstream>*/
#define OO 1e10
//std::ifstream cin("input.txt");
//std::ofstream cout("output.txt");
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
//cout<<fixed<<setprecision(*)<<***<<endl;
//a=97  A=65
/*scanf("%d",&..);
printf("%d",..);*/

using namespace std;
vector<int>vec,vec1(1);
int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie();
	bool b=false;
	string w,s,q; cin>>w>>s;
    bool arr[256]={0};
    for(int i=0;i<w.size();i++)
    {
        arr[w[i]]= true;
    }
    if((int)s.find("*")!=-1)
    {
        b=true;
    }
    int n;
    cin>>n;
    bool f;
    while(n--)
    {
        cin>>q;
        f=true;
        int l=q.size()-s.size()+1;
        if(l<0)
        {
            f=false;
        }
        else if(!b && l!=1)
        {
            f=false;
        }
        else
        {
            for(int i=0,j=0;i<s.size();i++,j++)
            {
                if(s[i]=='*')
                {
                    for(int k=0;k<l;k++)
                    {
                        if(arr[q[j]])
                        {
                            f=false;
                        }
                        j++;
                    }
                    j--;
                }
                else if(s[i]=='?')
                {
                    if(!arr[q[j]])
                    {
                        f=false;
                    }
                }
                else if(s[i]!=q[j])
                {
                    f=false;
                }
            }
        }

        if(f)
        {
            cout<<"YES"<<endl;
        }
        else
        {
            cout<<"NO"<< endl;
        }

        continue;
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;

int pos;

void getStarPos(){
    int i;
    for(i=0;str[i];++i){
        if(str[i]=='*'){
            pos=i;
            return;
        }
    }
    str[i]='*';
    pos=i;
    str[i+1]=0;
}

bool allBad(char*s,int st,int ed){
    while(st<=ed){
        if(good[s[st]-'a']){
            return false;
        }
        ++st;
    }
    return true;
}

bool checkPre(int ed){
    for(int i=0;i<=ed;++i){
        if(str[i]=='?'&&good[p[i]-'a']){
            continue;
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int sufLen,int pLen){
    for(int k=0;k<sufLen;++k){
        int i=len-k-1;
        int j=pLen-k-1;
        if(i<0||j<0){
            return false;
        }
        if(str[i]=='?'&&good[p[j]-'a']){
            continue;
        }
        if(str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    if(lenP<len-1){
        return false;
    }
    return allBad(p,pos,lenP-len+pos)&&checkPre(pos-1)&&checkSuffix(len-pos-1,lenP);
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        getStarPos();
        len=strlen(str);
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;

int pos;

void getStarPos(){
    int i;
    for(i=0;str[i];++i){
        if(str[i]=='*'){
            pos=i;
            return;
        }
    }
    str[i]='*';
    pos=i;
    str[i+1]=0;
}

bool allBad(char*s,int st,int ed){
    while(st<=ed){
        if(good[s[st]-'a']){
            return false;
        }
        ++st;
    }
    return true;
}

bool checkPre(int ed){
    for(int i=0;i<=ed;++i){
        if(str[i]=='?'&&good[p[i]-'a']){
            continue;
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int sufLen,int pLen){
    for(int k=0;k<sufLen;++k){
        int i=len-k-1;
        int j=pLen-k-1;
        if(str[i]=='?'&&good[p[j]-'a']){
            continue;
        }
        if(i<0||j<0||str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    if(lenP<len-1){
        return false;
    }
    return allBad(p,pos,lenP-len+pos)&&checkPre(pos-1)&&checkSuffix(len-pos,lenP);
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        getStarPos();
        len=strlen(str);
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;

bool vis[N];

bool allBad(){
    for(int i=0;p[i];++i){
        if((!vis[i])&&good[p[i]-'a']){
            return false;
        }
        if(!vis[i]&&i>0&&!vis[i-1]&&p[i]!=p[i-1]){
            return false;
        }
    }
    return true;
}

bool checkPre(){
    for(int i=0;str[i]&&str[i]!='*';++i){
        vis[i]=1;
        if(str[i]=='?'&&good[p[i]-'a']){
            continue;
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int pLen){
    int i=len-1;
    int j=pLen-1;
    for(;i>=0&&j>=0&&str[i]!='*';--i,--j){
        vis[j]=1;
        if(str[i]=='?'&&good[p[j]-'a']){
            continue;
        }
        if(str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    fill(vis,vis+lenP,0);
    if(lenP<len-1){
        return false;
    }
    return (checkPre()&&checkSuffix(lenP)&&allBad());
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        len=strlen(str);
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;

int pos;

void getStarPos(){
    int i;
    for(i=0;str[i];++i){
        if(str[i]=='*'){
            pos=i;
            return;
        }
    }
    str[i]='*';
    pos=i;
    str[i+1]=0;
}

bool allBad(char*s,int st,int ed){
    while(st<=ed){
        if(good[s[st]-'a']){
            return false;
        }
        ++st;
    }
    return true;
}

bool checkPre(int ed){
    for(int i=0;i<=ed;++i){
        if(str[i]=='?'&&good[p[i]-'a']){
            continue;
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int sufLen,int pLen){
    for(int k=0;k<sufLen;++k){
        int i=len-k-1;
        int j=pLen-k-1;
        if(str[i]=='?'&&good[p[j]-'a']){
            continue;
        }
        if(i<0||j<0||str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    if(lenP<len-1){
        return false;
    }
    return allBad(p,pos,lenP-len+pos)&&checkPre(pos-1)&&checkSuffix(len-1-pos,lenP);
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        getStarPos();
        len=strlen(str);
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;
bool star;
int vis[N];

bool allBad(){
    for(int i=0;p[i];++i){
        if((!vis[i])&&good[p[i]-'a']){
            return false;
        }
        if(vis[i]>2-star){
            return false;
        }
    }
    return true;
}

bool checkPre(){
    for(int i=0;str[i]&&str[i]!='*';++i){
        vis[i]++;
        if(str[i]=='?'&&good[p[i]-'a']){
            continue;
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int pLen){
    int i=len-1;
    int j=pLen-1;
    for(;i>=0&&j>=0&&str[i]!='*';--i,--j){
        vis[j]++;
        if(str[i]=='?'&&good[p[j]-'a']){
            continue;
        }
        if(str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    fill(vis,vis+lenP,0);
    if(lenP<len-1){
        return false;
    }
    return (checkPre()&&checkSuffix(lenP)&&allBad());
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        len=strlen(str);
        star=0;
        for(int i=0;str[i];++i){
            if(str[i]=='*'){
                star=1;
            }
        }
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf = 1e9+7;
const int N = 1e5 + 50;

bool good[26];
char str[N];
char p[N];
int len;

bool vis[N];

bool allBad(){
    for(int i=0;p[i];++i){
        if((!vis[i])&&good[p[i]-'a']){
            return false;
        }
    }
    return true;
}

bool checkPre(){
    for(int i=0;str[i]&&str[i]!='*';++i){
        vis[i]=1;
        if(str[i]=='?'){
            if(good[p[i]-'a']){
                continue;
            }
            else{
                return false;
            }
        }
        if(str[i]!=p[i]){
            return false;
        }
    }
    return true;
}

bool checkSuffix(int pLen){
    for(int k=0;len-k-1>=0&&str[len-k-1]!='*';++k){
        int i=len-k-1;
        int j=pLen-k-1;
        if(i<0||j<0){
            break;
        }
        vis[j]=1;
        if(str[i]=='?'){
            if(good[p[j]-'a']){
                continue;
            }
            else{
                return false;
            }
        }
        if(str[i]!=p[j]){
            return false;
        }
    }
    return true;
}

bool query(){
    int lenP=strlen(p);
    fill(vis,vis+lenP,0);
    if(lenP<len-1){
        return false;
    }
    return checkPre()&&checkSuffix(lenP)&&allBad();
}

int main(){
    //freopen("/home/lu/code/r.txt","r",stdin);
    //freopen("/home/lu/code/w.txt","w",stdout);
    while(~scanf("%s",str)){
        MEM(good,0);
        for(int i=0;str[i];++i){
            good[str[i]-'a']=1;
        }
        scanf("%s",str);
        len=strlen(str);
        int q;
        scanf("%d",&q);
        while(q--){
            scanf("%s",p);
            puts(query()?"YES":"NO");
        }
    }
    return 0;
}