#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9+7;
int n;
const int d = 5000;
const int N = 10005;
int bit[N];

void upd(int pos,int val){

	for(; pos < N; pos |= pos + 1)
		bit[pos] += val;

}

int query(int pos){

    int res = 0;
	for(; pos >= 0; pos = (pos & (pos + 1)) - 1)
		res += bit[pos];
	return res;

}

int query(int l,int r){
	return query(r)-query(l-1);
}

vector<pair<int,int> > ver[N],hor[N];
vector<int> D[N];

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    cin>>n;

    for(int i=0;i<n;i++){

    	int x1,y1,x2,y2;
    	cin>>x1>>y1>>x2>>y2;

    	x1+=d,x2+=d,y1+=d,y2+=d;

    	if(y1==y2)
    	  hor[y1].push_back(make_pair(min(x1,x2) , max(x1,x2)));
    	else
		  ver[x1].push_back(make_pair(min(y1,y2) , max(y1,y2)));

	}

	long long ans = 0;

	for(int i=0;i<N;i++)
	   for(auto pp1:hor[i]){

	      for(int i = 0; i < N; ++i) D[i].clear();
	      memset(bit, 0, sizeof bit);

	      for(int x=pp1.first;x<=pp1.second;x++){

			for(auto pp2:ver[x])
		     if(pp2.first<=i && pp2.second>i){
	      	  upd(x,1);
			  D[pp2.second].push_back(x);
	         }

		  }

	   for(int j=i+1;j<N;j++){

	   	 for(auto pp:hor[j]){

		  int curr = query(pp.first,pp.second);

	   	  ans += curr * (curr-1) / 2;

	     }

	   	 for(auto x:D[j])
	   	   upd(x,-1);

	   }

	  }

	cout<<ans;

    return 0;

}


 #include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define int long long
#define ll long long
#define mp make_pair
#define ss second
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
int n;
const int d = 5000;
const int N = 10005;
int bit[N];

void upd(int x,int val){

	for(int i=x;i<N;i+=(i&(-i)))
	   bit[i]+=val;

}

int query(int x){

    int q = 0;

	for(int i=x;i>0;i-=(i&(-i)))
  	   q+=bit[i];

  	return q;

}

int query(int l,int r){
	return query(r)-query(l-1);
}

vector<pii> ver[N],hor[N];
vector<int> D[N];

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    cin>>n;

    for(int i=0;i<n;i++){

    	int x1,y1,x2,y2;
    	cin>>x1>>y1>>x2>>y2;

    	x1+=d,x2+=d,y1+=d,y2+=d;

    	if(y1==y2)
    	  hor[y1].pb(mp(min(x1,x2) , max(x1,x2)));
    	else
		  ver[x1].pb(mp(min(y1,y2) , max(y1,y2)));

	}

	ll ans = 0;

	for(int i=0;i<N;i++){

	   for(auto pp1:hor[i]){

	      memset(bit, 0, sizeof bit);

	      for(int x=pp1.ff;x<=pp1.ss;x++){

			for(auto pp2:ver[x])
		     if(pp2.ff<=i && pp2.ss>i){
	      	  upd(x,1);
			  D[pp2.ss].pb(x);
	         }

		  }

	   for(int j=i+1;j<N;j++){

	   	 for(auto pp:hor[j]){

		  int curr = query(pp.ff,pp.ss);

	   	  ans += curr * (curr-1) / 2;

	     }

	   	 for(auto x:D[j])
	   	   upd(x,-1);

	   }

	  }

	}

	cout<<ans;

    return 0;

}


 #include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
//#define int long long
#define ll int
#define mp make_pair
#define ss second
#define pb push_back
#define ff first
#define um unordered_map
#define us unordered_set
#define pll pair<ll,ll>
#define large 100005
const int mod = 1e9+7;
#define PI 3.1415926535
ll powmod(ll base,ll exp,ll MOD){ll res=1;while(exp>0){if(exp%2==1) res=(res*base)%MOD;base=(base*base)%MOD;exp/=2;}return (res%MOD);}
int n;
const int d = 5000;
const int N = 10002;
int bit[N];

void upd(int i,int val){

	for(;i<N;i+=(i&(-i)))
	   bit[i]+=val;

}

int query(int i){

    int q = 0;

	for(;i>0;i-=(i&(-i)))
  	   q+=bit[i];

  	return q;

}

int query(int l,int r){
	return query(r)-query(l-1);
}

vector<pii> ver[N],hor[N];
vector<int> D[N];

int32_t main() {

    ios_base::sync_with_stdio(0);
	cin.tie(0);
    cout.tie(0);

    cin>>n;

    for(int i=0;i<n;i++){

    	int x1,y1,x2,y2;
    	cin>>x1>>y1>>x2>>y2;

    	x1+=d,x2+=d,y1+=d,y2+=d;

    	if(y1==y2)
    	  hor[y1].pb(mp(min(x1,x2) , max(x1,x2)));
    	else
		  ver[x1].pb(mp(min(y1,y2) , max(y1,y2)));

	}

	long long ans = 0;

	for(int i=0;i<N;i++)
	   for(auto pp1:hor[i]){

	      memset(bit, 0, sizeof bit);
	      for(int i = 0; i < N; ++i) D[i].clear();

	      for(int x=pp1.ff;x<=pp1.ss;x++){

			for(auto pp2:ver[x])
		     if(pp2.ff<=i && pp2.ss>i){
	      	  upd(x,1);
			  D[pp2.ss].pb(x);
	         }

		  }

	   for(int j=i+1;j<N;j++){

	   	 for(auto pp:hor[j]){

		  int curr = query(pp.ff,pp.ss);

	   	  ans += curr * (curr-1) / 2;

	     }

	   	 for(auto x:D[j])
	   	   upd(x,-1);

	   }

	  }

	cout<<ans;

    return 0;

}


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

int sign[30];

char a[30];
char s[100005];
char t[100005];
int n;

int main()
{
    std::ios::sync_with_stdio(false);

    memset(sign, 0, sizeof(sign));

    cin >> a;

    int len = strlen(a);

    for(int i = 0; i < len; i++){
        sign[a[i] - 'a'] = 1;
    }

    cin >> s;
    int lens = strlen(s);
    cin >> n;
    while(n--)
    {
        cin >> t;
        len = strlen(t);

        bool flag = 1;
        int sum = 0;

        int flag2 = 0;
        for(int i = 0; i < lens; i++) {
            if(s[i] == '*') {
                flag2 = 1;
                if(strlen(t) != strlen(s) && strlen(t)+1 != strlen(s)) {
                    flag = 0;
                    break;
                }
            }
        }

        if(!flag2 && strlen(t) != strlen(s)) flag = 0;

        if(flag == 0) {
            cout << "NO" << endl;
            continue;
        }

        for(int i = 0; i < len; i++){
            if(s[i+sum] != t[i]){
                if(s[i+sum] == '*' && strlen(s) > strlen(t)) {
                        sum = 1;
                }
                if(s[i+sum] == '?' && sign[t[i]-'a'] == 0){
                    flag = 0;
                    break;
                }
                else if(s[i+sum] >='a' && s[i+sum] <='z'){
                    flag = 0;
                    break;
                }
            }
        }

        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;

    }

}


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

int sign[30];

char a[30];
char s[100005];
char t[100005];
int n;

int main()
{
    std::ios::sync_with_stdio(false);

    memset(sign, 0, sizeof(sign));

    cin >> a;

    int len = strlen(a);

    for(int i = 0; i < len; i++){
        sign[a[i] - 'a'] = 1;
    }

    cin >> s;
    cin >> n;
    while(n--)
    {
        cin >> t;
        len = strlen(t);

        bool flag = 1;
        int sum = 0;

        int flag2 = 0;
        for(int i = 0; i < len; i++) {
            if(s[i] == '*') {
                flag2 = 1;
                if(strlen(t) != strlen(s) && strlen(t)+1 != strlen(s)) {
                    flag = 0;
                    break;
                }
            }
        }

        if(!flag2 && strlen(t) > strlen(s)) flag = 0;

        if(flag == 0) {
            cout << "NO" << endl;
            continue;
        }

        for(int i = 0; i < len; i++){
            if(s[i+sum] != t[i]){
                if(s[i+sum] == '*' && strlen(s) > strlen(t)) {
                        sum = 1;
                }
                if(s[i+sum] == '?' && sign[t[i]-'a'] == 0){
                    flag = 0;
                    break;
                }
                else if(s[i+sum] >='a' && s[i+sum] <='z'){
                    flag = 0;
                    break;
                }
            }
        }

        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;

    }

}


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

int sign[30];

char a[30];
char s[100005];
char t[100005];
int n;

int main()
{
    std::ios::sync_with_stdio(false);

    memset(sign, 0, sizeof(sign));

    cin >> a;

    int len = strlen(a);

    for(int i = 0; i < len; i++){
        sign[a[i] - 'a'] = 1;
    }

    cin >> s;
    int lens = strlen(s);
    cin >> n;
    while(n--)
    {
        cin >> t;
        len = strlen(t);

        bool flag = 1;
        int sum = 0;

        int flag2 = 0;
        int x = 0;

        for(int i = 0; i < len; i++){
            if(s[i+sum] != t[i+x]){
                if(s[i+sum] == '*'  ) {
                       if(strlen(s) == strlen(t)){
                            if(sign[t[i+x]='a'])  {
                                flag = 0;
                                break;
                            }
                            continue;
                        }
                        if(strlen(s) - 1 == strlen(t)) {
                            sum = 1;
                            i--;
                            continue;
                        }
                        sum = 1;
                        x = len - lens + 1;
                       // cout << "x=" << i << endl;
                        for(int j = i; j < i+x; j++){
                            //cout << "!!";
                            if(sign[t[j] - 'a']) {
                                //    cout <<"?";
                                flag = 0;
                                break;
                            }
                        }
                        if(!flag) break;
                }

                if(s[i+sum] == '?' && sign[t[i+x]-'a'] == 0){
                    flag = 0;
                    break;
                }
                else if(s[i+sum] >='a' && s[i+sum] <='z'){
                    flag = 0;
                    break;
                }
            }
        }

        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;

    }

}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (abs(m-len)>1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
					else i++,boo=0;
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo&&m+boo==len) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			bool booo=1;
			for (int i=0,j=0;i<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[j]-'a']) {bo=0; break;}
					else j--;
				}
				else
					if (a[i]!=s[j]) {bo=0; break;}
			}
			for (int i=0,j=0;i<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (bo||booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	bool ljj=0;
	for (int i=0;i<len;i++) if (a[i]=='*') ljj=1;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			if (!ljj) {puts("NO"); continue;}
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=0;
		for (int i=0;i<m;i++)
		{
			if (a[i]=='?')
			{
				if (!f[s[i]-'a']) {bo=0; break;}
			}
			else if (a[i]=='*')
			{
				if (f[s[i]-'a']) {bo=0; break;}
				else i++,boo=1;
			}
			else
				if (a[i]!=s[i]) {bo=0; break;}
		}
		if (bo&&m-boo==len) puts("YES"); else puts("NO");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[N];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	bool ljj=0;
	for (int i=0;i<len;i++) if (a[i]=='*') ljj=1;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			if (!ljj) {puts("NO"); continue;}
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	bool ljj=0;
	for (int i=0;i<len;i++) if (a[i]=='*') ljj=1;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			if (!ljj) {puts("NO"); continue;}
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[j]-'a']) {bo=0; break;}
					else j--;
				}
				else
					if (a[i]!=s[j]) {bo=0; break;}
			}
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (bo||booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[N];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	bool ljj=0;
	for (int i=0;i<len;i++) if (a[i]=='*') ljj=1;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1) {puts("NO"); continue;}
		if (m>=len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					for (int j=0;j<=m-len;j++)
						if (f[s[i+j]-'a']) {bo=0; break;}
					if (!bo) break;
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			if (!ljj) {puts("NO"); continue;}
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[26];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1||m>len) {puts("NO"); continue;}
		if (m==len)
		{
			for (int i=0;i<m;i++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[i]-'a']) {bo=0; break;}
				}
				else
					if (a[i]!=s[i]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					if (f[s[j]-'a']) {bo=0; break;}
					else j--;
				}
				else
					if (a[i]!=s[j]) {bo=0; break;}
			}
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[i]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (bo||booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<map>
#include<set>
#include<vector>
#define mp make_pair
#define fi first
#define se second
#define sqr(x) (x)*(x)
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define per(i,x,y) for (int i=(x);i>=(y);i--)
using namespace std;
typedef long long LL;
typedef double DBD;
typedef pair<int,int> pa;
const int inf=1e9;
const LL INF=1e18;
//-----------------------------------------------head-------------------------------------------//
const int N=1e5+10;
int n,f[N];
char a[N],s[N];
int Write[20];
int read() {int d=0,f=1; char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1; c=getchar();} while (c>='0'&&c<='9') d=(d<<3)+(d<<1)+c-48,c=getchar(); return d*f;}
void write(int x){int t=0; if (x<0) putchar('-'),x=-x; for (;x;x/=10) Write[++t]=x%10; if (!t) putchar('0'); for (int i=t;i>=1;i--) putchar((char)(Write[i]+48));}
void judge(){freopen(".in","r",stdin); freopen(".out","w",stdout);}
int main()
{
	//judge();
	scanf("%s",a);
	int len=strlen(a);
	for (int i=0;i<len;i++) f[a[i]-'a']=1;
	scanf("%s",a);
	len=strlen(a);
	bool ljj=0;
	for (int i=0;i<len;i++) if (a[i]=='*') ljj=1;
	scanf("%d",&n);
	while (n--)
	{
		scanf("%s",s);
		int m=strlen(s); bool bo=1,boo=1;
		if (m<len-1) {puts("NO"); continue;}
		if (m>=len)
		{
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {bo=0; break;}
				}
				else if (a[i]=='*')
				{
					for (int k=0;k<=m-len;k++)
						if (f[s[k+j]-'a']) {bo=0; break;}
					if (!bo) break;
					j+=m-len;
				}
				else
					if (a[i]!=s[j]) {bo=0; break;}
			}
			if (bo) puts("YES"); else puts("NO");
		}
		else
		{
			if (!ljj) {puts("NO"); continue;}
			bool booo=1;
			for (int i=0,j=0;j<m;i++,j++)
			{
				if (a[i]=='?')
				{
					if (!f[s[j]-'a']) {booo=0; break;}
				}
				else if (a[i]=='*')
				{
					j--;
				}
				else
					if (a[i]!=s[j]) {booo=0; break;}
			}
			if (booo) puts("YES"); else puts("NO");
		}
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(str) - 1;
		int len = strlen(str);
		for(;i < len;i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a])
				a++;
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1)
				a++;
			else{
				flag = 0;
				break;
			}
		}
		for(;j >= 0;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b])
				b--;
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1)
				b--;
			else{
				flag = 0;
				break;
			}
		}
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		int j = 0;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1){
				j++;
				continue;
			}
			else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0){
				continue;
			}else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1){
				j++;
				i--;
				continue;
			}
			else if(ch[i] == str[j]){
				j++;
				continue;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str1[30],str2[100005];
char str[100005];
int good[30];
int main(){
	int n,flag;
	while(scanf("%s",str1) != EOF){
		scanf("%s",str2);
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		memset(good,0,sizeof(good));
		for(int i = 0;i < len1;i++){
			good[str1[i] - 'a'] = 1;
		}
		scanf("%d",&n);
		while(n--){
			scanf("%s",str);
			flag = 1;
			int j = 0;
			int len = strlen(str);
			for(int i = 0;i < len2;i++){
		//		printf("%c ---- %c i = %d  j = %d\n",str2[i],str[j],i,j);
				if(i == len2 - 1 && str2[i] == '*' && j == len)
					break;
				if(j > len){
					flag = 0;
					break;
				}
				if(str2[i] == str[j]){
					j++;
				}else{
					if(str2[i] == '?' && good[str[j] - 'a']){

						j++;
					}else if(str2[i] == '*'){
				//		printf("len2 - i = %d  len - j = %d\n",len2 - i,len - j);
						if(!good[str[j] - 'a']){
							if(len2 - i > len - j)
								continue;
							else if(len2 - i == len - j){
								j++;
								continue;
							}
							j++;
							i--;
					//		printf("* to %c j = %d\n",str[j - 1],j);
						}else if(j == len - 1){
							j++;
						}else{
					//		printf("continue\n");
							continue;
						}
					}else{
						flag = 0;
						break;
					}
				}
			}
			if(j < len - 1)
				flag = 0;
		//	printf("j = %d\n",j);
			if(flag)
				printf("YES\n");
			else
			printf("NO\n");
		}

	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = 0;

		for(int i = 0; i < strlen(ch); i++) {
			//	printf("is %c %c\n",str[i],ch[i]);
			if(j >= strlen(str)) {
				flag = 0;
				break;
			}
			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1) {
				j++;
			} else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0) {
				continue;
			} else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1) {
				j++;
				i--;
				continue;
			} else if(ch[i] == str[j]) {
				j++;
				continue;
			} else {
				flag = 0;
				break;
			}
		}
		if(j == strlen(str) - 2 && str[j + 1] != '*'){
			flag = 0;
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		int j = 0;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(j >= strlen(str)){
				flag = 0;
				break;
			}

			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1){
				j++;
			}
			else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0){
				continue;
			}else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1){
				j++;
				i--;
				continue;
			}
			else if(ch[i] == str[j]){
				j++;
				continue;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		int j = 0;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1){
				j++;
			}
			else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0){
				j++;
				continue;
			}else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1){
				j++;
				i--;
			}
			else if(ch[i] == str[j]){
				j++;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[i] == '?' && k[ch[i] - 'a' + 1] == 1){
			//	printf("? k = %d\n",k[ch[i] - 'a' + 1]);
				continue;
			}
			else if(str[i] == '*' && k[ch[i] - 'a' + 1] == 0){
				continue;
			}else if(str[i] == '*' && k[ch[i] - 'a' + 1] == 1 && str[i + 1] == '?'){
				continue;
			}
			else if(ch[i] == str[i])
				continue;
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str1[30],str2[100005];
char str[100005];
int good[30];
int main(){
	int n,flag;
	while(scanf("%s",str1) != EOF){
		scanf("%s",str2);
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		memset(good,0,sizeof(good));
		for(int i = 0;i < len1;i++){
			good[str1[i] - 'a'] = 1;
		}
		scanf("%d",&n);
		while(n--){
			scanf("%s",str);
			flag = 1;
			int j = 0;
			int len = strlen(str);
			for(int i = 0;i < len2;i++){
			//	printf("%c ---- %c i = %d  j = %d\n",str2[i],str[j],i,j);
				if(i == len2 - 1 && str2[i] == '*' && j == len - 1 && good[str[j]]){
					flag = 0;
					break;
				}
				if(i == len2 - 1 && str2[i] == '*' && j == len)
					break;
				if(j > len){
					flag = 0;
					break;
				}
				if(str2[i] == str[j]){
					j++;
				}else{
					if(str2[i] == '?' && good[str[j] - 'a']){

						j++;
					}else if(str2[i] == '*'){
			//			printf("len2 - i = %d  len - j = %d\n",len2 - i,len - j);
						if(!good[str[j] - 'a'] && j != len - 1){
							if(len2 - i > len - j)
								continue;
							else if(len2 - i == len - j){
								j++;
								continue;
							}
							j++;
							i--;
				//			printf("* to %c j = %d\n",str[j - 1],j);
						}else if(j == len - 1 && !good[str[j] - 'a']){
							j++;
						}else{
							if(j == len - 1)
								flag = 0;
				//			printf("continue\n");
							continue;
						}
					}else{
						flag = 0;
						break;
					}
				}
			}
			if(j < len - 1)
				flag = 0;
		//	printf("j = %d\n",j);
			if(flag)
				printf("YES\n");
			else
			printf("NO\n");
		}

	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int len = strlen(str);
	//	printf("a = %d b = %d i = %d j = %d\n",a,b,i,j);
		for(;i < len;i++){

			if(str[i] == '*')
				break;
			if(str[i] == ch[a])
				a++;
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1)
				a++;
			else{
				flag = 0;
				break;
			}
		}
		for(;j >= 0;j--){
	//		printf("%c %c\n",str[j],ch[b]);
			if(str[j] == '*')
				break;
			if(str[j] == ch[b])
				b--;
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1)
				b--;
			else{
				flag = 0;
				break;
			}
		}
	//	printf("a = %d b = %d i = %d j = %d\n",a,b,i,j);
		a--;
		if(a > b)
			flag = 0;
		for(int o = a + 1;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
		//		printf("*");
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int cnt = 0;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a]){
				a++;
			}
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1){
				a++;
			}
			else{
				flag = 0;
				break;
			}
		}

		for(;j >= i;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b] && a <= b){
				b--;
			}
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1 && a <= b){
				b--;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(i != j && i < j){
			flag = 0;
	//		printf("***\n");
		}

	//	printf("%d %d %d %d\n",a,b,i,j);
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1 && str[i] == str[j] && str[i] == '*'){
				flag = 0;

				break;
			}else if(str[i] != '*'){
				flag = 0;
	//			printf("***\n");
				break;
			}
		}

		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int cnt = 0;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a]){
				a++;
			}
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1){
				a++;
			}
			else{
				flag = 0;
				break;
			}
		}
		for(;j >= i;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b] && a <= b){
				b--;
			}
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1 && a <= b){
				b--;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(i != j && i < j){
	//		printf("***\n");
			flag = 0;
		}

	//	printf("%d %d %d %d\n",a,b,i,j);
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
	//			printf("***\n");
				break;
			}
		}

		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = 0;

		for(int i = 0; i < strlen(ch); i++) {
			//	printf("is %c %c\n",str[i],ch[i]);
			if(j >= strlen(str)) {
				flag = 0;
				break;
			}
			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1) {
				j++;
			} else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0) {
				continue;
			} else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1) {
				j++;
				i--;
				continue;
			} else if(ch[i] == str[j]) {
				j++;
				continue;
			} else {
				flag = 0;
				break;
			}
		}
		if(j == strlen(str) - 2 && str[j + 1] != '*'){
			flag = 0;
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
//...


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	for(int i = 1;i <= 26;i++){
		if(k[i] == 1)
			printf("%c ",i + 'a' - 1);
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[i] == '?' && k[ch[i] - 'a' + 1] == 1){
			//	printf("? k = %d\n",k[ch[i] - 'a' + 1]);
				continue;
			}
			else if(str[i] == '*')
				continue;
			else if(ch[i] == str[i])
				continue;
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
char str1[30],str2[100005];
char str[100005];
int good[30];
int main(){
	int n,flag;
	while(scanf("%s",str1) != EOF){
		scanf("%s",str2);
		int len1 = strlen(str1);
		int len2 = strlen(str2);
		memset(good,0,sizeof(good));
		for(int i = 0;i < len1;i++){
			good[str1[i] - 'a'] = 1;
		}
		scanf("%d",&n);
		while(n--){
			scanf("%s",str);
			flag = 1;
			int j = 0;
			int len = strlen(str);
			for(int i = 0;i < len2;i++){
			//	printf("%c ---- %c i = %d  j = %d\n",str2[i],str[j],i,j);

				if(i == len2 - 1 && str2[i] == '*' && j == len)
					break;
				if(j > len){
					flag = 0;
					break;
				}
				if(str2[i] == str[j]){
					j++;
				}else{
					if(str2[i] == '?' && good[str[j] - 'a']){
						j++;
					}else if(str2[i] == '*'){
			//			printf("len2 - i = %d  len - j = %d\n",len2 - i,len - j);
						if(!good[str[j] - 'a'] && j != len - 1){
							if(len2 - i > len - j)
								continue;
							else if(len2 - i == len - j){
								j++;
								continue;
							}
							j++;
							i--;
				//			printf("* to %c j = %d\n",str[j - 1],j);
						}else if(j == len - 1 && !good[str[j] - 'a']){
							j++;
						}else{
							if(i == len2 - 1)
								flag = 0;
				//			printf("continue\n");
							continue;
						}
					}else{
						flag = 0;
						break;
					}
				}
			}
			if(j < len - 1)
				flag = 0;
		//	printf("j = %d\n",j);
			if(flag)
				printf("YES\n");
			else
			printf("NO\n");
		}

	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[i] == '?' && k[ch[i] - 'a' + 1] == 1){
			//	printf("? k = %d\n",k[ch[i] - 'a' + 1]);
				continue;
			}
			else if(str[i] == '*')
				continue;
			else if(ch[i] == str[i])
				continue;
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a])
				a++;
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1)
				a++;
			else{
				flag = 0;
				break;
			}
		}
		for(;j >= 0;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b])
				b--;
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1)
				b--;
			else{
				flag = 0;
				break;
			}
		}
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}
//...


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int cnt = 0;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a]){
				a++;
			}
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1){
				a++;
			}
			else{
				flag = 0;
				break;
			}
		}

		for(;j >= i;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b] && a <= b){
				b--;
			}
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1 && a <= b){
				b--;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(i != j && i < j){
			flag = 0;
	//		printf("***\n");
		}

	//	printf("%d %d %d %d\n",a,b,i,j);
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
	//			printf("***\n");
				break;
			}
		}

		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int cnt = 0;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a]){
				a++;
			}
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1){
				a++;
			}
			else{
				flag = 0;
				break;
			}
		}

		for(;j >= i;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b] && a <= b){
				b--;
			}
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1 && a <= b){
				b--;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(i != j && i < j){
			flag = 0;
	//		printf("***\n");
		}

	//	printf("%d %d %d %d\n",a,b,i,j);
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1 && str[i] == str[j] && str[i] == '*'){
				flag = 0;
	//			printf("***\n");
				break;
			}else if(str[i] != '*' && k[ch[o] - 'a' + 1] == 0){
				flag = 0;
				break;
			}
		}

		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main(){
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0;i < strlen(c);i++){
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--){
		scanf("%s",ch);
		flag = 1;
		int j = 0;
		for(int i = 0;i < strlen(ch);i++){
		//	printf("is %c %c\n",str[i],ch[i]);
			if(str[j] == '?' && k[ch[i] - 'a' + 1] == 1){
				j++;
				continue;
			}
			else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 0){
				j++;
				continue;
			}else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1 && str[i + 1] == '?'){
				j += 2;
				continue;
			}else if(str[j] == '*' && k[ch[i] - 'a' + 1] == 1 && str[i + 1] == ch[i]){
				j += 2;
				continue;
			}
			else if(ch[i] == str[j]){
				j++;
				continue;
			}
			else{
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main() {
	char c[50];
	int k[50],flag;
	memset(k,0,sizeof(k));
	char str[100005];
	char ch[100005];
	scanf("%s",c);
	for(int i = 0; i < strlen(c); i++) {
		k[c[i] - 'a' + 1] = 1;
	}
	scanf("%s",str);
	int n;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",ch);
		flag = 1;
		int j = strlen(str) - 1;
		int i = 0;
		int a = 0;
		int b = strlen(ch) - 1;
		int cnt = 0;
		for(;i < strlen(str);i++){
			if(str[i] == '*')
				break;
			if(str[i] == ch[a]){
				a++;
				cnt++;
			}
			else if(str[i] == '?' && k[ch[a] - 'a' + 1] == 1){
				a++;
				cnt++;
			}
			else{
				flag = 0;
				break;
			}
		}
		for(;j >= 0;j--){
			if(str[j] == '*')
				break;
			if(str[j] == ch[b]){
				b--;
				cnt++;
			}
			else if(str[j] == '?' && k[ch[b] - 'a' + 1] == 1){
				b--;
				cnt++;
			}
			else{
				flag = 0;
				break;
			}
		}
	//	printf("%d %d\n",a,b);
		if(cnt > strlen(ch) && a != strlen(str) && b != -1)
			flag = 0;
		for(int o = a;o <= b;o++){
			if(k[ch[o] - 'a' + 1] == 1){
				flag = 0;
				break;
			}
		}
		if(flag)
			printf("YES\n");
		else
			printf("NO\n");
	}
	return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!='*'){
        if(s[i]!=ne[j]){
            return 0;
        }
    }
    return 1;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens!=len){
                flag=0;
                break;
            }
            for(int i=0;i<len;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
        }
        else{
            int j=0;
            if(lens<len){ //* add
               for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                        j++;
                    }
                    else{
                        for(int k=i;k<=i+len-lens;k++){
                            if(good[ne[k]-'a']==1){
                                flag=0;
                                break;
                            }
                        }
                    }
                    j=i+len-lens+1;
               }
            }
            else if(lens==len){ //*=one word
                for(int i=0;i<len;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else{
                        if(good[ne[j]-'a']==1){
                            flag=0;
                            break;
                        }
                    }
                    j++;
               }
            }
            else if(lens==len+1){
                for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else j--;
                    j++;
               }
            }
            else flag=0;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n,stmark;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!='*'){
        if(s[i]!=ne[j]){
            return 0;
        }
    }
    return 1;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            stmark=i;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens!=len){
                flag=0;
                break;
            }
            for(int i=0;i<len;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
        }
        else{
            for(int i=0;i<stmark;i++){
                if(s[i]=='?'){
                    if(good[ne[i]-'a']==0){
                        flag=0;
                        break;
                    }
                }
                else if(s[i]!=ne[i]){
                    flag=0;
                    break;
                }
            }
            int k=stmark,l=lens-1;
            for(;l>stmark;l--){
                if(l+len-lens<0){
                    flag=0;
                    break;
                }
                if(check(l,l+len-lens)==0){
                    flag=0;
                    break;
                }
            }
            for(int i=stmark;i<=stmark+len-lens;i++){
                if(good[ne[i]-'a']==1){
                    flag=0;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n,stmark;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!=ne[j]) return 0;
    return 1;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            stmark=i;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens==len){
                for(int i=0;i<len;i++){
                    if(check(i,i)==0){
                        flag=0;
                        break;
                    }
                }
            }
            else flag=0;
        }
        else{
            if(lens<=len+1){
                if(flag){
                        for(int i=0;i<stmark;i++){
                        if(check(i,i)==0){
                            flag=0;
                            break;
                        }
                    }
                }
                if(flag){
                    for(int i=stmark+1;i<lens;i++){
                        if(i+len-lens<0||check(i,i+len-lens)==0){
                            flag=0;
                            break;
                        }
                    }
                }
                if(flag){
                    for(int i=stmark;i<=stmark+len-lens;i++){
                        if(good[ne[i]-'a']==1){
                            flag=0;
                            break;
                        }
                    }
                }
            }
            else flag=0;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n,stmark;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!=ne[j]) return 0;
    return 1;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            stmark=i;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens==len){
                for(int i=0;i<len;i++){
                    if(check(i,i)==0){
                        flag=0;
                        break;
                    }
                }
            }
            else flag=0;
        }
        else{
            if(flag){
                    for(int i=0;i<stmark;i++){
                    if(check(i,i)==0){
                        flag=0;
                        break;
                    }
                }
            }
            if(flag){
                for(int i=stmark+1;i<lens;i++){
                    if(i+len-lens<0||check(i,i+len-lens)==0){
                        flag=0;
                        break;
                    }
                }
            }
            if(flag){
                for(int i=stmark;i<=stmark+len-lens;i++){
                    if(good[ne[i]-'a']==1){
                        flag=0;
                        break;
                    }
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!='*'){
        if(s[i]!=ne[j]){
            return 0;
        }
    }
    return 1;
}

int main(){
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens!=len){
                flag=0;
                break;
            }
            for(int i=0;i<len;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
        }
        else{
            int j=0;
            if(lens<len){
               for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else{
                        for(int k=i;k<=i+len-lens;k++){
                            if(good[ne[k]-'a']==1){
                                flag=0;
                                break;
                            }
                        }
                    }
                    j=i+len-lens+1;
               }
            }
            else if(lens==len){
                for(int i=0;i<len;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else{
                        if(good[ne[j]-'a']==1){
                            flag=0;
                            break;
                        }
                    }
                    j++;
               }
            }
            else if(lens==len+1){
                for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else j--;
                    j++;
               }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n,stmark;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!=ne[j]) return 0;
    return 1;
}

int main(){
    //freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            stmark=i;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens!=len){
                flag=0;
                break;
            }
            for(int i=0;i<len;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
        }
        else{
            for(int i=0;i<stmark;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
            for(int i=stmark+1;i<lens;i++){
                if(i+len-lens<0||check(i,i+len-lens)==0){
                    flag=0;
                    break;
                }
            }
            for(int i=stmark;i<=stmark+len-lens;i++){
                if(good[ne[i]-'a']==1){
                    flag=0;
                    break;
                }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,n;

int main(){
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    scanf("%d",&n);
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        for(int i=0;i<len;i++){
            if(s[i]=='?'){
                if(good[ne[i]-'a']==0){
                    flag=0;
                    break;
                }
            }
            else if(s[i]=='*'){
                if(good[ne[i]-'a']==1){
                    flag=0;
                    break;
                }
            }
            else if(ne[i]!=s[i]){
                flag=0;
                break;
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


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

bool good[26]; //good=1
char cgood[30];
char s[100009];
char ne[100009];
int len,lens,n;

bool check(int i,int j){
    if(s[i]=='?'){
        if(good[ne[j]-'a']==0){
            return 0;//0=err
        }
    }
    else if(s[i]!='*'){
        if(s[i]!=ne[j]){
            return 0;
        }
    }
    return 1;
}

int main(){
    freopen("in.txt","r",stdin);
    scanf("%s",cgood);
    len=strlen(cgood);
    for(int i=0;i<len;i++){
        good[cgood[i]-'a']=1;
    }
    scanf("%s",s);
    lens=strlen(s);
    scanf("%d",&n);
    bool st=0;
    for(int i=0;i<lens;i++){
        if(s[i]=='*'){
            st=1;
            break;
        }
    }
    while(n--){
        scanf("%s",ne);
        len=strlen(ne);
        bool flag=1;
        bool star=0;
        if(st==0){
            if(lens!=len){
                flag=0;
                break;
            }
            for(int i=0;i<len;i++){
                if(check(i,i)==0){
                    flag=0;
                    break;
                }
            }
        }
        else{
            int j=0;
            if(lens<len){
               for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else{
                        for(int k=i;k<=i+len-lens;k++){
                            if(good[ne[k]-'a']==1){
                                flag=0;
                                break;
                            }
                        }
                    }
                    j=i+len-lens+1;
               }
            }
            else if(lens==len){
                for(int i=0;i<len;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else{
                        if(good[ne[j]-'a']==1){
                            flag=0;
                            break;
                        }
                    }
                    j++;
               }
            }
            else if(lens==len+1){
                for(int i=0;i<lens;i++){
                    if(s[i]!='*'){
                        if(check(i,j)==0){
                            flag=0;
                            break;
                        }
                    }
                    else j--;
                    j++;
               }
            }
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
        for(int i=0;i<len;i++) ne[i]='\0';
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        getchar();
        while(n--){
            getline(cin,x);
            i=j=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else break;
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){

                        while(m[x[j]]!=1){
                                if(j==x.length()){
                                break;}
                                j++;

                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0,f=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                        f=1;
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<x.length()-S.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                else {
                        flag=1;
                        break;
                }
            }
            if((flag==0 && f==1) || (f==0 && S.length()==x.length()))cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<x.length()-S.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                else {
                        flag=1;
                        break;
                }
            }
            if(flag==0)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        getchar();
        while(n--){
            getline(cin,x);
            i=j=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else break;
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                        while(m[x[j]]!=1){
                                if(j==x.length()){
                                break;}
                                j++;

                                    }
                                    }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            continue;
                                    }
                    else{
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<S.length()-x.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                                    }
                else {
                        break;
                }
            }
            if(flag==0)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else break;
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            continue;
                                    }
                    else{
                            if(x.length()<S.length())continue;
                            else{while(m[x[j]]!=1 && x[j]!=S[i+1]){
                                if(j==x.length()){

                                break;}
                                j++;
                        }
                            }
                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            for(i=0,j=0;i<S.length();i++,j++){
                if(S[i]=='?'){
                    if(m[x[j]]==1)continue;
                    else break;
                }
                else if(S[i]==x[j])continue;
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            j--;
                            continue;
                                    }
                    else{
                        while(m[x[j]]!=1){
                                if(j==x.length()){
                                j--;
                                break;}
                                j++;
                        }
                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else break;
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            continue;
                                    }
                    else{
                            if(x.length()<S.length())continue;
                            else{while(m[x[j]]!=1){
                                if(j==x.length()){

                                break;}
                                j++;
                        }
                            }
                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else break;
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            continue;
                                    }
                    else{
                        while(m[x[j]]!=1){
                                if(j==x.length()){
                                break;}
                                j++;
                        }
                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            continue;
                                    }
                    else{
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<S.length()-x.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                                    }
                else {
                        flag=1;
                        break;
                }
            }
            if(flag==0)cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<x.length()-S.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                else {
                        flag=1;
                        break;
                }
            }
            if(flag==0 && S.length()==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            for(i=0,j=0;i<S.length();i++,j++){
                if(S[i]=='?'){
                    if(m[x[j]]==1)continue;
                    else break;
                }
                else if(S[i]==x[j])continue;
                else if(S[i]=='*'){
                    if(m[x[j]]==1){
                            j--;
                            continue;
                                    }
                    else{
                        while(m[x[j]]!=1){
                                if(j==x.length()){
                                j--;
                                break;}
                                j++;
                        }
                        if(j+1!=x.length())j--;
                                    }
                                    }
                else {
                        break;
                }
            }
            if(i==S.length() && j==x.length())cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
        string s,S,x;
        int n,flag,i,j;
        map<char,int>m;
        cin>>s>>S>>n;
        for(int i=0;i<s.length();i++){
            m[s[i]]=1;
        }
        while(n--){
            cin>>x;
            i=j=0;
            int flag=0,f=0;
            for(i=0,j=0;i<S.length();i++){
                if(S[i]=='?'){
                    if(m[x[j]]==1){j++;continue;}
                    else {flag=1;break;}
                }
                else if(S[i]==x[j]){j++;continue;}
                else if(S[i]=='*'){
                        f=1;
                            if(x.length()<S.length())continue;
                            else if(x.length()==S.length()){
                                if(m[x[i]]==1){flag=1;break;}
                                else j++;
                        }
                        else{
                            for(int k=0;k<x.length()-S.length()+1;k++){
                                if(m[x[j]]==1){flag=1;break;}
                                else j++;
                            }
                        }
                            }
                else {
                        flag=1;
                        break;
                }
            }
            if((flag==0 && f==1) || (f==0 && S.length()==x.length() && flag==0))cout<<"YES"<<endl;
            else cout<<"NO"<<endl;
        }
        return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100001

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    if(strlen(s)!=strlen(sp)){return false;}
    int bro=0;
    for(int i=0; i<strlen(sp); i++){
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]!='?' && s[bro]!='*' && s[bro]!=sp[i]){return false;}
        if((s[bro]=='*' && dobar(sp[i]))|| s[bro]!='*'){bro++;}

    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); ){
        if(s[bro]==sp[i] && s[bro]!='?' && s[bro]!='*'){bro++; i++;}
        if(s[bro]!=sp[i] && s[bro]!='?' && s[bro]!='*'){return false;}

        if(s[bro]=='?' && dobar(sp[i])){bro++; i++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}

        if(s[bro]=='*' && !dobar(sp[i])){k++; i++;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k>0){bro++; i--;}

    }
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int MAXL = 1e5+5;

char S[MAXL], P[MAXL], Q[MAXL];
int q;
bool good[MAXL];

int main() {
	scanf("%s", S);
	for (int i = 0; S[i]; ++i) {
		good[S[i]-'a'] = 1;
	}
	scanf("%s", P);
	int lp = strlen(P);
	scanf("%d", &q);
	for (int i = 0; i < q; ++i) {
		scanf("%s", Q);
		int lq = strlen(Q);
		bool ok = 1;
		int it = 0;
		for (int j = 0; j < lq && ok; ++j, ++it) {
			if (it == lp) {
				ok = 0;
				break;
			}
			char c = P[it];
			if (c == '?')	ok = good[Q[j]-'a'];
			if ('a' <= c && c <= 'z') ok = c == Q[j];
			if (c == '*') {
				for (int k = 0; k <= lq-lp && ok; ++k) {
					ok = !good[Q[j++]-'a'];
				}
				j--;
			}
		}
		if (P[it] == '*') it++;
		printf("%s\n", ok && it == lp ? "YES" : "NO");
	}

	return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[27];
char s[duljina];
bool good[duljina];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
    int i=0,bro=0,j;
    while(true){
        if('a'<=s[bro] && s[bro]<='z' && sp[i]==s[bro]){bro++; i++;}
        if('a'<=s[bro] && s[bro]<='z' && sp[i]!=s[bro]){return false;}

        if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
        if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

        if(s[bro]=='*'){
            for(j=i; j<(i+h+1); j++){
                if(good[sp[i]-'a']){return false;}
            }
            i=j;
            bro++;
        }

        if(i>=strlen(sp)){break;}
    }
}
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];
bool good[10000];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
    int k=0,i=0,bro=0,j;
    while(true){
        if('a'<=sp[i] && sp[i]<='z' && sp[i]==sp[bro]){bro++; i++;}
        if('a'<=sp[i] && sp[i]<='z' && sp[i]!=sp[bro]){return false;}

        if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
        if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

        if(s[bro]=='*'){
            for(j=i; j<(i+h+1); j++){
                if(good[sp[i]-'a']){return false;}
            }
            i=j;
            bro++;
        }

        if(i>=strlen(sp)){break;}
    }
}
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];
bool good[1000];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
    int k=0,i=0,bro=0,j;
    while(true){
        if(s[bro]==sp[i] && s[bro]!='?' && s[bro]!='*'){bro++; i++;}
        if(s[bro]!=sp[i] && s[bro]!='?' && s[bro]!='*'){return false;}

        if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
        if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

        if(s[bro]=='*'){
            for(j=i; j<(i+h+1); j++){
                if(good[sp[i]-'a']){return false;}
            }
            i=j;
            bro++;
        }

        if(i>=strlen(sp)){break;}
    }
}
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[27];
char s[duljina];
bool good[duljina];

int n;
char sp[duljina];

int pro_zlo(char *sp){
    int i=0,bro=0,j;
    int h=strlen(sp)-strlen(s);
    if(h<-1){return false;}else{
        while(true){
            if('a'<=s[bro] && s[bro]<='z' && sp[i]==s[bro]){bro++; i++;}
            if('a'<=s[bro] && s[bro]<='z' && sp[i]!=s[bro]){return false;}

            if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
            if(s[bro]=='?' && !good[sp[i]-'a']){return false;}

            if(s[bro]=='*'){
                for(j=i; j<(i+h+1); j++){
                    if(good[sp[i]-'a']){return false;}
                }
                i=j;
                bro++;
            }

            if(i>=strlen(sp)){break;}
        }

    }

if(strlen(s)==bro && i==strlen(sp)){return true;}else{return false;}
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
    }

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if( pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); ){
        if(s[bro]==sp[i] && s[bro]!='?' && s[bro]!='*'){bro++; i++;}
        if(s[bro]!=sp[i] && s[bro]!='?' && s[bro]!='*'){return false;}

        if(s[bro]=='?' && dobar(sp[i])){bro++; i++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}

        if(s[bro]=='*' && !dobar(sp[i])){k++; i++;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k>0){bro++; i--;}

    }
    if(bro!=strlen(s) && bro!=strlen(s)-1){return false;}
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[duljina], s[duljina], sp[duljina];
int n;
int good[duljina];

int main() {
	scanf("%s", dobri);

	for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
	}

    scanf("%s",s);
    int ls = strlen(s), lsp, bro,i,z,k;
    scanf("%d", &n);

    for(int j=0; j<n; j++){
        scanf("%s", sp);
        lsp = strlen(sp);
        i=0;
        bro=0;
        if(lsp-ls<-1){printf("NO\n");}else{
            i=0;
            bro=0;
            k=1;
            while(k){
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]==sp[i]){bro++; i++;}
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]!=sp[i]){printf("NO\n"); k=0;}

                if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
                if(s[bro]=='?' && !good[sp[i]-'a']){printf("NO\n"); k=0;}

                if(s[bro]=='*'){
                    for(z=i; z<i+(lsp-ls)+1; z++){
                        if(good[sp[z]-'a']){printf("NO\n"); k=0; break;}
                    }
                    bro++;
                    i+=z;
                }

                if(k==0){break;}
                if(i>=lsp){printf("YES\n"); k=0; break;}
            }

        }

    }
return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); i++){
        if(s[bro]==sp[i]){bro++;}else{return false;}
        if(s[bro]=='?' && dobar(sp[i])){bro++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]=='*' && !dobar(sp[i])){k++;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k>0){bro++; i--;}
    }
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); ){
        if(s[bro]==sp[i] && s[bro]!='?' && s[bro]!='*'){bro++; i++;}
        if(s[bro]!=sp[i] && s[bro]!='?' && s[bro]!='*'){return false;}

        if(s[bro]=='?' && dobar(sp[i])){bro++; i++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}

        if(s[bro]=='*' && !dobar(sp[i])){k++; i++;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k>0){bro++; i--;}

    }
    if(bro!=strlen(s)){return false;}
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    if(strlen(sp)<strlen(s)){return false;}else{
    int k=0;
    for(int i=0; i<strlen(sp); i+=0){
        if(s[bro]==sp[i]){bro++; i++;}else{return false;}
        if(s[bro]=='?' && dobar(sp[i])){bro++; i++;}
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]=='*' && !dobar(sp[i])){k++; i++;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k>0){bro++; i--;}
    }
    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}


#include <cstring>
#include <cstdio>

const int duljina = 1e5+5;

char dobri[duljina], s[duljina], sp[duljina];
int n;
int good[duljina];

int main() {
	scanf("%s", dobri);

	for(int i=0; i<strlen(dobri); i++){
        good[dobri[i]-'a']=1;
	}

    scanf("%s",s);
    int ls = strlen(s), lsp, bro,i,z,k;
    scanf("%d", &n);

    for(int j=0; j<n; j++){
        scanf("%s", sp);
        lsp = strlen(sp);
        i=0;
        bro=0;
        if(lsp-ls<-1){printf("NO\n");}else{
            i=0;
            bro=0;
            k=1;
            while(k){
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]==sp[i]){bro++; i++;}
                if('a' <= s[bro] && s[bro] <= 'z' && s[bro]!=sp[i]){printf("NO\n"); k=0;}

                if(s[bro]=='?' && good[sp[i]-'a']){bro++; i++;}
                if(s[bro]=='?' && !good[sp[i]-'a']){printf("NO\n"); k=0;}

                if(s[bro]=='*'){
                    for(z=i; z<i+(lsp-ls)+1; z++){
                        if(good[sp[z]-'a']){printf("NO\n"); k=0; break;}
                    }
                    bro++;
                    i+=z;
                }

                if(k==0){break;}
                if(i>=lsp && bro>=ls){printf("YES\n"); k=0; break;}
            }

        }

    }
return 0;
}


#include <string.h>
#include <cstdio>
#define duljina 100002

char dobri[27];
char s[duljina];

int n;
char sp[duljina];

bool dobar(char c){
    for(int i=0; i<strlen(dobri); i++){
        if(c==dobri[i]){return true;}
    }
return false;
}

bool pro_prazna(char *sp){
    if(strlen(s)-1!=strlen(sp)){return false;}
        int bro=0;
        for(int i=0; i<strlen(sp); i++){
            if(s[bro]=='*'){bro++;}
            if(s[bro]=='?' && s[bro]!='*'  && !dobar(sp[i])){return false;}
            if(s[bro]!='?' && s[bro]!=sp[i]){return false;}
            bro++;

    }
return true;
}

int pro_zlo(char *sp){
    int bro=0;
    int k=0;
    for(int i=0; i<strlen(sp); i++){
        if(s[bro]=='?' && !dobar(sp[i])){return false;}
        if(s[bro]!='?' && s[bro]!='*' && s[bro]!=sp[i]){return false;}
        if(s[bro]=='*' && dobar(sp[i]) && k==0){return false;}
        if(s[bro]=='*' && !dobar(sp[i])){k++;}
        if((s[bro]=='*' && dobar(sp[i]))|| s[bro]!='*'){bro++;}

    }
return true;
}

int main(void){

    scanf("%s", dobri);
    scanf("%s", s);
    scanf("%d", &n);

    for(int i=0; i<n; i++){

        scanf("%s", sp);
        if(pro_prazna(sp)|| pro_zlo(sp)){printf("YES\n");}else{printf("NO\n");}

    }

return 0;
}