#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*' && r==0) r=1;

			if (s1.size()==s.size()+1)
			{
				r=0;
				x++;
			}

			if (r)
			{

				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1,x++;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss) cout<<"YES"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
map<int,int>F;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s1.size(); j++)
			F[j]=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*' && r==0) r=1;

			if (s1[x]=='*' && s1.size()==s.size()+1)
			{
				r=0;
				x++;
				F[x]=1;
			}

			if (r)
			{
				if (f[s[j]])
				{
					cout<<"NO"<<endl;
					ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1,x++;
				F[x]=1;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) F[x]=1,x++;
						else
				{
					cout<<"NO"<<endl;
					ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) F[x]=1,x++;
					else
				{
					cout<<"NO"<<endl;
					ss=1;
					break;
				}

				continue;
			}
		}

	//	for (int j=0; j<s1.size(); j++)
		//	cout<<F[j]<<" ";
	//	cout<<endl;

		if (!ss)
			for (int j=0; j<s1.size(); j++)
				if (!F[j] && s1[j]!='*')
				{
					cout<<"NO"<<endl;
					ss=1;
					break;
				}

		if (!ss) cout<<"YES"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*' && r==0) r=1;

			if (s1[x]=='*' && s1.size()==s.size()+1)
			{
				r=0;
				x++;
			}

			if (r)
			{

				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1,x++;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss && s1.size()==x) cout<<"YES"<<endl;
					        else cout<<"NO"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*' && r==0) r=1;

			if (r)
			{
				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1,x++;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss) cout<<"YES"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*' && r==0) r=1;

			if (s1[x]=='*' && s1.size()==s.size()+1)
			{
				r=0;
				x++;
			}

			if (r)
			{

				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1,x++;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss && s1.size()>=x) cout<<"YES"<<endl;
					        else
		if (!ss) cout<<"NO"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*') r=1;

			if (r)
			{
				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()) r=-1;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss) cout<<"YES"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
long long n,k,ss;
map<char,int>f;
string s1,s2,s;
main ()
{
	cin>>s2;

	for (int i=0; i<s2.size(); i++)
		f[s2[i]]=1;

	cin>>s1;

	cin>>n;

	for (int i=1; i<=n; i++)
	{
		cin>>s;

		int x=0,r=0;
		ss=0;

		for (int j=0; j<s.size(); j++)
		{
			if (s1[x]=='*') r=1;

			if (r)
			{
				if (f[s[j]])
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				if (r==s.size()-s1.size()+1) r=-1;
				r++;
				continue;
			}

			if (r) continue;

			if ('a'<=s1[x] && s1[x]<='z')
			{
				if (s1[x]==s[j]) x++;
						else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}

			if (s1[x]=='?')
			{
				if (f[s[j]]) x++;
					else
				{
					cout<<"NO"<<endl; ss=1;
					break;
				}

				continue;
			}
		}

		if (!ss) cout<<"YES"<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        getchar();
        while(q--)
        {
            gets(str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1==pattern.size() && (indx==(int)pattern.size()-1 || indx==0) )
                    goto jump;

                if(pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

jump:
            ;

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

//        getchar();
        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1 !=pattern.size() && pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

cba
?*cbc
1
acbc
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        getchar();
        while(q--)
        {
            gets(str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1==pattern.size())
                    goto jump;

                if(pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

jump:
            ;

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

cba
?*cbc
1
acbc
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        getchar();
        while(q--)
        {
            gets(str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1==pattern.size())
                    goto jump;

                if(pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

jump:
            ;

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

cba
?*cbc
1
acbc
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[27];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=25; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);

            bool ok=1;
            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1 , i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
             len1=sz, len2=0;

             lenn1=len1 , lenn2=len2;

            for(i=0; len1 ; i++ , len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=i;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor-- , len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            cerr<<len1<<" "<<len2<<endl;

            len2=sz-(lenn1+lenn2);
            cerr<<len2<<endl;

            for(i=k; len2; i++ , len2--)
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[27];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=25; i++)
            isgood[i]=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);

            if(pattern.size()>=sz && pattern.size()<=sz+1)
            {
                bool yes=0;
                int i=0,j=sz-1,k=2,mm,ok=1;
                int indx=0;
                for(mm=i; mm<=j; mm++, indx++)
                    if(pattern[mm]=='?')
                    {
                        if(isgood[str[indx]-'a']==0)
                        {
                            debug("NO 1 m=%d indx=%d\t",mm,indx);
                            ok=0;
                            break;
                        }
                    }
                    else if(pattern[mm]!='*')
                    {
                        if(str[indx]!=pattern[mm])
                        {
                            debug("NO 2 m=%d indx=%d\t",mm,indx) ;
                            ok=0;
                            break;
                        }
                    }

                if(pattern.size()==sz && ok)
                {
                    yes=1;
                    goto vag;
                }

                if(ok && mm<pattern.size() && pattern[mm]=='*')
                {
                    yes=1;
                    goto vag;
                }

                if(pattern.size()==sz+1)
                {
                    ok=1;
                    indx=0;
                    i=1, j=sz;

                    for(mm=i; mm<=j; mm++, indx++)
                        if(pattern[mm]=='?')
                        {
                            if(isgood[str[indx]-'a']==0)
                            {
                                debug("NO 1 m=%d indx=%d\t",mm,indx);
                                ok=0;
                                break;
                            }
                        }
                        else if(pattern[mm]!='*')
                        {
                            if(str[indx]!=pattern[mm])
                            {
                                debug("NO 2 m=%d indx=%d\t",mm,indx) ;
                                ok=0;
                                break;
                            }
                        }

                    if(ok && pattern[0]=='*')
                    {
                        yes=1;
                        goto vag;
                    }

                }

vag:
                ;

                if(yes)
                    printf("YES\n");
                else
                    printf("NO\n");

            }
            else
                printf("NO\n");

        }

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/
#define shesh   {printf("NO\n");continue;}

int q,x,y,sz,isgood[1000];
string good,pattern,str;

int main()
{
    while(cin>>good>>pattern>>q)
    {
        memset(isgood,0,sizeof(isgood));

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        indx=pattern.find('*');
        string s1,s2;
        if(indx!=-1)
            s1=pattern.substr(0,indx), s2=pattern.substr(indx+1);
        else
            s1=pattern, s2="";

        debug("%s %s\n",s1.c_str(),s2.c_str());

        while(q--)
        {
            cin>>str;
            bool ok=1;

            if(indx==-1 && s1.size()+s2.size() != str.size())
                shesh;
            if(indx!=-1  && s1.size()+s2.size() > str.size())
                shesh;

            for(int i=0; i<s1.size(); i++)
                if(s1[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                        ok=0;
                }
                else if(s1[i]!=str[i])
                    ok=0;

            if(!ok)
                shesh;

            for(int i=(int)s2.size()-1, cursor=str.size()-1 ; i>=0 ; i--, cursor--)
                if(s2[i]=='?')
                {
                    if(!isgood[str[cursor]-'a'])
                        ok=0;
                }
                else if(s2[i]!=str[cursor])
                    ok=0;

            if(!ok)
                shesh;

            for(int i=s1.size(); i<str.size()-s2.size(); i++)
                if(isgood[str[i]-'a'])
                    ok=0;

            if(!ok)
                shesh;

            printf((ok)?"YES\n":"NO\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

cba
?*cbc
1
acbc

b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
yes

b
a*b
1
acd efb
yes

b
ab?cd
1
abxcde
no

b
ab*cd
1
abcd
yes

b
abcd*
1
abcd
yes

b
abcdefghi*
1
abcd
no

b
ab*cdefghi
1
abcd
no

*/


//#pragma optimize("00")
#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf(__VA_ARGS__)
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        memset(isgood,0,sizeof(isgood));

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        debug("%s %s %d\n",good.c_str(),pattern.c_str(),q);

        getchar();

        while(q--)
        {
            gets(str);

            debug("%s\n",str);

            sz=strlen(str);
            if(sz<pattern.size())
            {
                printf("NO\n");
                continue;
            }

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
yes

b
a*b
1
acd efb
yes

b
ab?cd
1
abxcde
no
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        getchar();
        while(q--)
        {
            gets(str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1==pattern.size() && indx==(int)pattern.size()-1)
                    goto jump;

                if(pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

jump:
            ;

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<len1<<" "<<len2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);
            if(!star  && pattern.size()!=sz)
            {
                printf("NO\n");
                continue;
            }

            if(star)
            {
                if(sz+1 !=pattern.size() && pattern.size()>sz)
                {
                    printf("NO\n");
                    continue;
                }
            }

            bool ok=1;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            len2=sz-(lenn1+lenn2);

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd

b
abcd*
2
abcd
abcd xyzcdefg
yes
yes

cba
?*cbc
1
acbc

b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
yes

b
a*b
1
acd efb
yes

b
ab?cd
1
abxcde
no

b
ab*cd
1
abcd
yes

b
abcd*
1
abcd
yes

b
abcdefghi*
1
abcd
no

b
ab*cdefghi
1
abcd
no

*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[27];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=25; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1 , i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
             len1=sz, len2=0;

             lenn1=len1 , lenn2=len2;

            for(i=0; len1 ; i++ , len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=i;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor-- , len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<len1<<" "<<len2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;

            for(i=k; len2; i++ , len2--)
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[27];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=25; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);

            bool ok=1;
            int indx1,indx2,cursor=pattern.size()-1;
            if(star)
                indx1=indx-1, indx2=sz-((int)pattern.size()-1-indx);
            else
                indx1=sz-1, indx2=sz;

            for(int i=0; i<=indx1; i++)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(int i=sz-1; i>=indx2 ; i--, cursor--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            for(int i=indx1+1; i<indx2; i++)
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=999; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        getchar();
        while(q--)
        {
            gets(str);
            sz=strlen(str);

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<len1<<" "<<len2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf( __VA_ARGS__ )
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[27];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        for(int i=0; i<=25; i++)
            isgood[i]=0;

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        while(q--)
        {
            scanf("%s",str);
            sz=strlen(str);

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

//            cerr<<len1<<" "<<len2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
*/


//#pragma optimize("00")
#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf(__VA_ARGS__)
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        memset(isgood,0,sizeof(isgood));

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        debug("%s %s %d\n",good.c_str(),pattern.c_str(),q);

        getchar();

        while(q--)
        {
            gets(str);

            debug("\n%s\n",str);

            sz=strlen(str);
            if(sz<pattern.size() && !(pattern.size()==sz+1 && pattern[pattern.size()-1]=='*'))
            {
                debug("1. ");
                printf("NO\n");
                continue;
            }

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                debug("2. ");
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                debug("3. ");
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                debug("4. ");
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
yes

b
a*b
1
acd efb
yes

b
ab?cd
1
abxcde
no
*/


//#pragma optimize("00")
#include<bits/stdc++.h>
using namespace std;

//#define debug(...)   printf(__VA_ARGS__)
#define debug(...)   /****nothing****/

#define BREAK {ok=0;break;}
#define ll long long
#define ull unsigned long long
#define pb push_back
#define mem(arr,val)    memset(arr,val,sizeof(arr))
#define mp make_pair
#define pii pair<int,int>
#define F first
#define S second
#define sz(x) (int)(x).size()
#define nopos string::npos
int q,x,y,sz,isgood[1000];
string good,pattern;
char str[100006];

int main()
{
    while(cin>>good>>pattern>>q)
    {
        memset(isgood,0,sizeof(isgood));

        int indx,star=0;

        for(int i=0; i<good.size(); i++)
            isgood[good[i]-'a']=1;

        for(int i=0; i<pattern.size(); i++)
            if(pattern[i]=='*')
            {
                indx=i;
                star=1;
                break;
            }

        debug("%s %s %d\n",good.c_str(),pattern.c_str(),q);

        getchar();

        while(q--)
        {
            gets(str);

            debug("\n%s\n",str);

            sz=strlen(str);
            if( (!star && sz!=pattern.size()) || (star && sz<pattern.size() && indx<(int)pattern.size()-1) )
            {
                debug("1. ");
                printf("NO\n");
                continue;
            }

            bool ok=1;
//            cerr<<indx<<" "<<star<<endl;

            int len1,len2,lenn1,lenn2,cursor=pattern.size()-1, i, k;

            if(star)
                len1=indx, len2=(int)pattern.size()-1-indx;
            else
                len1=sz, len2=0;

            lenn1=len1, lenn2=len2;

            for(i=0; len1 ; i++, len1--)
                if(pattern[i]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[i]!=str[i])
                {
                    ok=0;
                    break;
                }

            k=lenn1;

            if(!ok)
            {
                debug("2. ");
                printf("NO\n");
                continue;
            }

//            cerr<<indx1<<" "<<indx2<<endl;

//                cerr<<indx1<<" "<<indx2<<endl;

            for(i=sz-1; len2 ; i--, cursor--, len2--)
                if(pattern[cursor]=='?')
                {
                    if(!isgood[str[i]-'a'])
                    {
                        ok=0;
                        break;
                    }
                }
                else if(pattern[cursor]!=str[i])
                {
                    ok=0;
                    break;
                }

            if(!ok)
            {
                debug("3. ");
                printf("NO\n");
                continue;
            }

//            cerr<<lenn1<<" "<<lenn2<<endl;

            len2=sz-(lenn1+lenn2);
//            cerr<<len2<<endl;
//             cout<<k<<" "<<str[1]<<endl;

            for(i=k; len2; i++, len2--)
            {
//                cout<<str[i]<<" ";
                if(isgood[str[i]-'a'])
                {
                    ok=0;
                    break;
                }

            }

//            cout<<endl;

            if(!ok)
            {
                debug("4. ");
                printf("NO\n");
                continue;
            }

            if(!ok)
            {
                printf("NO\n");
            }
            else
                printf("YES\n");

        }

    }

    return 0;
}
/*
b
a*aaaa??cd
1
axyzcdefaaaaaabbcd
yes

b
a*b
1
acd efb
yes

b
ab?cd
1
abxcde
no

b
ab*cd
1
abcd
*/


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m & j < k and sample[j] != '*'; j++)
		{
			if((sample[j] != '?' and sample[j] != a[i]) or !(PRESENT(c, a[i])))
				ans = 0;
			i++;
		}
		for(; i >= 0 & jj >= 0 and sample[jj] != '*'; jj--)
		{
			if((sample[jj] != '?' and sample[jj] != a[ii]) or !(PRESENT(c, a[ii])))
				ans = 0;
			ii--;
		}
		for(int l = i; l <= ii; l++)
		{
			if(PRESENT(c, a[l]))
				ans = 0;
		}
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= i and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		if(sample[jj] == '*' and sample[j] == '*')
			for(int l = i; l <= ii; l++)
			{
				if(PRESENT(c, a[l]))
					ans = 0;
			}
		else if(jj > j or i < m)
			ans = 0;
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= i and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		if(sample[jj] == '*' and sample[j] == '*')
			for(int l = i; l <= ii; l++)
			{
				if(PRESENT(c, a[l]))
					ans = 0;
			}
		else if(jj > j)
			ans = 0;
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= i and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		if(sample[jj] == '*' and sample[j] == '*')
			for(int l = i; l <= ii; l++)
			{
				if(PRESENT(c, a[l]))
					ans = 0;
			}
		else if(jj > j or i < m or j < k)
			ans = 0;
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= 0 and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		if(sample[jj] == '*' and sample[j] == '*')
			for(int l = i; l <= ii; l++)
			{
				if(PRESENT(c, a[l]))
					ans = 0;
			}
		else if(jj > j)
			ans = 0;
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m & j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= 0 & jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		for(int l = i; l <= ii; l++)
		{
			if(PRESENT(c, a[l]))
				ans = 0;
		}
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m & j < k and sample[j] != '*'; j++)
		{
			if((sample[j] != '?' and sample[j] != a[i]) or (sample[j] == '?' and !(PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= 0 & jj >= 0 and sample[jj] != '*'; jj--)
		{
			if((sample[jj] != '?' and sample[jj] != a[ii]) or (sample[jj] == '?' and !(PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		for(int l = i; l <= ii; l++)
		{
			if(PRESENT(c, a[l]))
				ans = 0;
		}
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= 0 and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		for(int l = i; l <= ii; l++)
		{
			if(PRESENT(c, a[l]))
				ans = 0;
		}
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if((sample[j] != '?' and sample[j] != a[i]) or !(PRESENT(c, a[i])))
				ans = 0;
			i++;
		}
		for(; ii >= 0 and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if((sample[jj] != '?' and sample[jj] != a[ii]) or !(PRESENT(c, a[ii])))
				ans = 0;
			ii--;
		}
		for(int l = i; l <= ii; l++)
		{
			if(PRESENT(c, a[l]))
				ans = 0;
		}
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ull unsigned long long int
#define il long int
#define ill long long int
#define ld long double
#define pi pair<int,int>
#define V(x) vector<x>
#define pb(x) push_back(x)
#define SORT(ls) sort(ls.begin(), ls.end())
#define DESC(ls) sort(ls.rbegin(), ls.rend());
#define mp(x, y) make_pair(x, y)
#define SIZE(x) int(x.size())
#define F(i,a,n) for(long int i=(a); i<(n); ++i)
#define FD(i,a,n) for(long int i=(a);i>=(n);--i)
#define S(x) scanf("%d",&x)
#define P(x) printf("%d\n",x)
#define BINSTR(num) bitset<64>((num)).to_string();
#define SI cin>>
#define DI(x, y) cin>>x>>y
#define TI(x, y, z) cin>>x>>y>>z
#define tr(...) cout<<__FUNCTION__<<' '<<__LINE__<<" = ";trace(#__VA_ARGS__, __VA_ARGS__)
void SO(){cout << endl;}
template<typename S, typename ... Strings>
void SO(S x, const Strings&... rest){cout<<x<<' ';SO(rest...);}
template<typename T>
void trace(const char* name, T&& arg1){cout<<name<<" : "<<arg1<<endl;}
template<typename T, typename... Args>
void trace(const char* names, T&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');cout.write(names, comma-names)<<" : "<<arg1<<" | ";trace(comma+1,args...);}
#define foreach(container, it) for(typeof(container.begin()) it = container.begin(); it != container.end(); it++)
#define ALL(c) c.begin(), c.end()
#define PRESENT(container, x) container.find(x) != container.end()
#define VPRESENT(container, element) (find(ALL(container),element) != container.end())
#define fr first
#define se second
#define FASTER ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define endl "\n"
/*
#define cin fin
#define cout fout
ifstream fin("input.txt");
ofstream fout("output.txt");
*/
#define inf 2e9;
const long double PI = 3.1415926535897932384626433832795;
const int v5 = 100000; //  (10**5)
const int v9 = 1000000000; //  (10**9)
const int v6 = 1000000; //  (10**6)
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
//USE THIS TO DEFINE TYPE EVERYWHERE
#define tp long long int

inline tp ABS(tp a) { if ( a < 0 ) return (-a); return a; }
inline tp min(tp a,tp b,tp c){return min(a,min(b,c));}
inline tp max(tp a,tp b,tp c){return max(a,max(b,c));}
//inline tp powmod(tp x,tp n, tp _mod){tp res=1;while(n){if(n&1)res=(res*x)%_mod;x=(x*x)%_mod;n>>=1;}return res;}
//inline tp gcd(tp a,tp b){tp t;while(b){a=a%b;t=a;a=b;b=t;}return a;}
//inline tp lcm(tp a,tp b){return (a*b)/(gcd(a,b));}
//inline int bitCount(unsigned int i) { return __builtin_popcount(i); }
inline void AI(tp x[], tp n){for(int i=0; i<(n); ++i)SI(x[i]);}
inline void VI(vector<tp> &arr, tp n){for(int i=0; i<(n); ++i){tp x;SI(x);arr.pb(x);};}
//CODE STARTS HERE-----------------------------------------------------------------------------------------------------------------------------------------------

const int AS = 4000001;

tp n, m,k;
map<char, int> c;
tp a[AS];
map< tp, V(tp) > g;

int main()
{
	FASTER;
	string good, sample;
	SI(good);
	for(int r = 0; r < good.size(); r++)
		c[good[r]] = 1;
	SI(sample);
	int k = sample.size();
	SI(n);
	F(iii,0,n)
	{
		string a;
		SI(a);
		int m = a.size(), i = 0, j = 0, ii = m-1, jj = k-1, ans = 1;
		for(; i < m and j < k and sample[j] != '*'; j++)
		{
			if(!(a[i] == sample[j] or (sample[j] == '?' and PRESENT(c, a[i]))))
				ans = 0;
			i++;
		}
		for(; ii >= i and jj >= 0 and sample[jj] != '*'; jj--)
		{
			if(!(a[ii] == sample[jj] or (sample[jj] == '?' and PRESENT(c, a[ii]))))
				ans = 0;
			ii--;
		}
		if(sample[jj] == '*' and sample[j] == '*')
			for(int l = i; l <= ii; l++)
			{
				if(PRESENT(c, a[l]))
					ans = 0;
			}
		else if(i < m)
			ans = 0;
		if(ans == 0)
			SO("NO");
		else
			SO("YES");
	}

	return 0;
}