#include<bits/stdc++.h>
using namespace std;
int main() {
string x,y;
cin>>x>>y;
int l,m,q,h[27]={0};
l=x.length();
m=y.length();
for(int i=0;i<l;i++)
h[x[i]-'a']=1;
cin>>q;
while(q--)
{
string z,tp;
cin>>z;
tp=y;
int n=z.length(),f=1;
if(m==n || m==n+1)
{
if(m==n+1)
{
if(tp.find('*')!=string::npos)
tp.erase(tp.find('*'));
}
int lx=tp.length();
//cout<<tp<<" "<<lx<<endl;
for(int i=0;i<lx;i++)
{
if(!(tp[i]=='?' || tp[i]=='*'))
{
if(tp[i]!=z[i]) f=0;
}
else
{
if(tp[i]=='?' && !h[z[i]-'a']) f=0;
if(tp[i]=='*' && h[z[i]-'a']) f=0;
}
}
if(f)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
else
cout<<"NO"<<endl;
}
return 0;
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBtYWluKCkgewoJc3RyaW5nIHgseTsKCWNpbj4+eD4+eTsKCWludCBsLG0scSxoWzI3XT17MH07CglsPXgubGVuZ3RoKCk7CgltPXkubGVuZ3RoKCk7Cglmb3IoaW50IGk9MDtpPGw7aSsrKQoJaFt4W2ldLSdhJ109MTsKCWNpbj4+cTsKCXdoaWxlKHEtLSkKCXsKCQlzdHJpbmcgeix0cDsKCQljaW4+Pno7CgkJdHA9eTsKCQlpbnQgbj16Lmxlbmd0aCgpLGY9MTsKCQlpZihtPT1uIHx8IG09PW4rMSkKCQl7CgkJIGlmKG09PW4rMSkKCQkgewoJCSAgaWYodHAuZmluZCgnKicpIT1zdHJpbmc6Om5wb3MpCgkgCSAgdHAuZXJhc2UodHAuZmluZCgnKicpKTsKCQkgfQoJCSBpbnQgbHg9dHAubGVuZ3RoKCk7CgkJIC8vY291dDw8dHA8PCIgIjw8bHg8PGVuZGw7CgkJIGZvcihpbnQgaT0wO2k8bHg7aSsrKQoJCSB7CgkJIAlpZighKHRwW2ldPT0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