#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

int n;
bool buc[30];

int main(){
	string good,pat;
	cin >> good >> pat;
	REP(i,good.size()) buc[good[i]-'a'] = true;
	cin >> n;
	while(n--){
		bool ok = true;
		string cur; cin >> cur;
		if(cur.size() > pat.size() ||
				pat.size()-cur.size() >= 2){
			puts("NO");
			continue;
		}

		int off = 0;
		REP(i,cur.size()){
			if(pat[i] != '?' && pat[i] != '*'){
				if(cur[i-off] != pat[i]){
					ok = false;
					break;
				}
			} else if(pat[i] == '?'){
				if(!buc[cur[i-off]-'a']){
					ok = false;
					break;
				}
			} else if(pat[i] == '*'){
				if(cur.size() != pat.size()) off++;
			}
		}
		if(ok) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

int n;
bool buc[30];

int main(){
	string good,pat;
	cin >> good >> pat;
	REP(i,good.size()) buc[good[i]-'a'] = true;
	cin >> n;
	while(n--){
		bool ok = true;
		string cur; cin >> cur;
		if(cur.size() > pat.size() ||
			(pat.size()-cur.size()) >= 2){
			puts("NO");
			continue;
		}

		int off = 0;
		REP(i,pat.size()){
			if(pat[i] != '?' && pat[i] != '*'){
				if(cur[i-off] != pat[i]){
					ok = false;
					break;
				}
			} else if(pat[i] == '?'){
				if(!buc[cur[i-off]-'a']){
					ok = false;
					break;
				}
			} else if(pat[i] == '*'){
				if(cur.size() != pat.size()) off++;
			}
		}
		if(ok) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

int n;
bool buc[400];

int main(){
	string good,pat;
	cin >> good >> pat;
	REP(i,good.size()) buc[good[i]] = true;
	scanf("%d%*c",&n);
	while(n--){
		bool ok = true;
		string cur;
		getline(cin,cur);
		int off = 1;
		int i = 0, j = 0;
		while(i < pat.size()){
			if(pat[i] != '?' && pat[i] != '*'){
				if(cur[j] != pat[i]){
					ok = false;
					break;
				}
			} else if(pat[i] == '?'){
				if(!buc[cur[j]]){
					ok = false;
					break;
				}
			} else if(pat[i] == '*'){
				while(j < cur.size()){
					if(!buc[cur[j]]){
						j++;
					} else break;
				}
				j--;
			}
			j++; i++;
		}
		if(ok && i == pat.size() && j == cur.size()) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

string pat;
bool buc[444],has;
int n;

bool test(string cur){
	if(has){
		int i = 0,j = cur.size()-1,k = pat.size()-1;
		while(pat[i] != '*'){
			if(i >= cur.size()) return false;
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
			i++;
		}
		while(pat[k] != '*'){
			if(j < 0) return false;
			if(pat[k] == '?'){
				if(!buc[cur[j]]) return false;
			} else {
				if(pat[k] != cur[j]) return false;
			}
			k--; j--;
		}
		if(j+1 < i) return false;
		for(int z = i; z <= j; z++) if(buc[cur[z]]) return false;
		return true;
	} else {
		if(pat.size() != cur.size()) return false;
		REP(i,pat.size()){
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
		}
		return true;
	}
}

int main(){
	cin >> pat; REP(i,pat.size()) buc[pat[i]] = true;
	cin >> pat;
	cin >> n;
	REP(i,pat.size()) if(pat[i] == '*') has = true;
	while(n--){
		string cur;
		cin >> cur;
		if(test(cur)) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

string pat;
bool buc[444],has;
int n;

bool test(string cur){
	if(has){
		int i = 0,j = cur.size()-1,k = pat.size()-1;
		while(pat[i] != '*'){
			if(i >= cur.size()) return false;
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
			i++;
		}
		while(pat[k] != '*'){
			if(j < 0) return false;
			if(pat[k] == '?'){
				if(!buc[cur[j]]) return false;
			} else {
				if(pat[k] != cur[j]) return false;
			}
			k--; j--;
		}
		for(int z = i; z <= j; z++) if(buc[cur[z]]) return false;
		return true;
	} else {
		if(pat.size() != cur.size()) return false;
		REP(i,pat.size()){
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
		}
		return true;
	}
}

int main(){
	cin >> pat; REP(i,pat.size()) buc[pat[i]] = true;
	cin >> pat;
	cin >> n;
	REP(i,pat.size()) if(pat[i] == '*') has = true;
	while(n--){
		string cur;
		cin >> cur;
		if(test(cur)) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

string pat;
bool buc[444],has;
int n;

bool test(string cur){
	if(!has){
		if(pat.size() != cur.size()) return false;
		REP(i,pat.size()){
			if(pat[i] != '?') {
				if(pat[i] != cur[i]) return false;
			} else {
				if(buc[cur[i]] == false) return false;
			}
		}
		return true;
	} else {
		int i = 0, j = 0;
		while(i < pat.size()){
			if(pat[i] == '*'){
				i++;
				while(j < cur.size() && !buc[cur[j]]) j++;
			} else {
				if(pat[i] != '?') {
					if(pat[i] != cur[j]) return false;
				} else {
					if(buc[cur[j]] == false) return false;
				}
				i++; j++;
			}
		}
		if(j != cur.size()) return false;
		return true;
	}
}

int main(){
	cin >> pat; REP(i,pat.size()) buc[pat[i]] = true;
	cin >> pat;
	cin >> n;
	REP(i,pat.size()) if(pat[i] == '*') has = true;
	while(n--){
		string cur;
		cin >> cur;
		if(test(cur)) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

string pat;
bool buc[444],has;
int n;

bool test(string cur){
	if(has){
		int i = 0,j = cur.size()-1,k = pat.size()-1;
		while(pat[i] != '*'){
			if(i >= cur.size()) return false;
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
			i++;
		}
		while(pat[k] != '*'){
			if(j < 0) return false;
			if(pat[k] == '?'){
				if(!buc[cur[j]]) return false;
			} else {
				if(pat[k] != cur[j]) return false;
			}
			k--; j--;
		}
		for(int z = i; z <= j; z++) if(buc[cur[z]]) return false;
		return true;
	} else {
		REP(i,pat.size()){
			if(pat[i] == '?'){
				if(!buc[cur[i]]) return false;
			} else {
				if(pat[i] != cur[i]) return false;
			}
		}
		return true;
	}
}

int main(){
	cin >> pat; REP(i,pat.size()) buc[pat[i]] = true;
	cin >> pat;
	cin >> n;
	REP(i,pat.size()) if(pat[i] == '*') has = true;
	while(n--){
		string cur;
		cin >> cur;
		if(test(cur)) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

int n;
bool buc[400];

int main(){
	string good,pat;
	cin >> good >> pat;
	REP(i,good.size()) buc[good[i]] = true;
	scanf("%d%*c",&n);
	while(n--){
		string cur;
		getline(cin,cur);
		int i = 0, j = 0;
		while(i < pat.size()){
			if(pat[i] != '?' && pat[i] != '*'){
				if(cur[j] == pat[i]){
					i++;
					j++;
				} else break;
			} else if(pat[i] == '?'){
				if(buc[cur[j]]){
					i++;
					j++;
				} else break;
			} else if(pat[i] == '*'){
				while(j < cur.size() && !buc[cur[j]]) j++;
				i++;
			}
		}
		if(i == pat.size() && j == cur.size()) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <bits/stdc++.h>

#define FOR(i, a, n) for(int i = (int)(a); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define all(a) a.begin(),a.end()
#define pb push_back
#define LSOne(S) (S & (-S))
#define dbg(x) cerr << ">>>> " << x << endl;
#define _ << " , " <<
#define mp make_pair
#define mt make_tuple
#define x first
#define y second
#define ii pair<int,int>
#define iii pair<int, ii>
#define lll pair<ll, pair<ll, ll> >
#define maxn 111111
#define PI 3.141592654
#define vi vector<ll>

typedef unsigned long long llu;
typedef long long int ll;
typedef long double ld;

const int INF = 0x3f3f3f3f;
const double EPS = 1e-6;

using namespace std;

int n;
bool buc[400];

int main(){
	string good,pat;
	cin >> good >> pat;
	REP(i,good.size()) buc[good[i]] = true;
	scanf("%d%*c",&n);
	while(n--){
		string cur;
		getline(cin,cur);
		int i = 0, j = 0;
		while(i < pat.size()){
			if(pat[i] != '?' && pat[i] != '*'){
				if(j < cur.size() && cur[j] == pat[i] && buc[cur[j]]){
					i++;
					j++;
				} else break;
			} else if(pat[i] == '?'){
				if(j < cur.size() && buc[cur[j]]){
					i++;
					j++;
				} else break;
			} else if(pat[i] == '*'){
				while(j < cur.size() && !buc[cur[j]]) j++;
				i++;
			}
		}
		if(i == pat.size() && j == cur.size()) puts("YES");
		else puts("NO");
	}
	return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*') != string::npos) {
        star = true;
    }
    //cout << star << endl;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length(); j++, z++) {
            if(s.length() > input.length() && !star) {
                possible = false;
                cout << "NO" << endl;
                break;
            } else if(star && s.length() - input.length() > 1) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() || j < s.length(); j++, z++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    z--;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*') != string::npos) {
        star = true;
    }
    //cout << star << endl;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        //cout << "s.length() - input.length() : " << s.length() - input.length() << endl;
        //cout << "star : " << star << endl;
        if(static_cast<int>(s.length() - input.length()) > 1 && star) {
            cout << "NO" << endl;
            continue;
        } else if(fabs(static_cast<int>(s.length() - input.length())) > 0 && !star) {
            //cout << "In second!" << endl;
            cout << "NO" << endl;
            continue;
        }

        for(int j = 0, z = 0; j < s.length() && z < input.length(); j++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                //cout << "j : " << j << endl;
                //cout << "str : " << input.substr(z, input.length()) << endl;
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    /*string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*')) {
        star = true;
    }
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() && j < s.length(); j++) {
            if(s.length() - input.length() > 0 && star) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }*/
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() || j < s.length(); j++, z++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() || j < s.length(); j++, z++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = false;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() && j < s.length(); j++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                } else {
                    possible = true;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                        possible = true;
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    possible = true;
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            } else {
                possible = true;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length(); j++, z++) {
            if(s[j] == '?') {
                if(!(found(input[j], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() && j < s.length(); j++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*')) {
        star = true;
    }
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() && j < s.length(); j++) {
            if(s.length() - input.length() > 0 && star) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*') != string::npos) {
        star = true;
    }
    //cout << star << endl;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length(); j++, z++) {
            if(s.length() > input.length() && !star) {
                possible = false;
                cout << "NO" << endl;
                break;
            } else if(star && s.length() - input.length() > 1) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            if(s[j] == '?') {
                if(!(found(input[j], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() || j < s.length(); j++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length(); j++, z++) {
            if(s.length() > input.length()) {
                possible = false;
                cout << "NO" << endl;
                break;
            }

            if(s[j] == '?') {
                if(!(found(input[j], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    if(input == "") {
        return false;
    }
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*') != string::npos) {
        star = true;
    }
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        if(s.length() - input.length() > 1 && star) {
            cout << "NO" << endl;
            continue;
        } else if(fabs(s.length() - input.length()) > 0 && !star) {
            cout << "NO" << endl;
            continue;
        }

        for(int j = 0, z = 0; j < s.length() && z < input.length(); j++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    /*string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;
    bool star = false;
    if(s.find('*')) {
        star = true;
    }
    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length() && j < s.length(); j++) {
            if(s.length() - input.length() > 0 && star) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                    continue;
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }

            z++;
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }*/
    return 0;
}


#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <iterator>
#include <queue>
#include <deque>
#include <functional>
#include <vector>

using namespace std;

bool found(char input, string good_chars) {
    for(int i = 0; i < good_chars.length(); i++) {
        if(input == good_chars[i]) {
            return true;
        }
    }

    return false;
}

bool found_str(string input, string good_chars) {
    for(int i = 0; i < input.length(); i++) {
        if(good_chars.find(input[i]) != string::npos) {
            return true;
        }
    }

    return false;
}

int main() {
    string good_chars, s, input;
    int n;
    cin >> good_chars >> s;
    cin >> n;

    for(int i = 0; i < n; i++) {
        bool possible = true;
        cin >> input;
        for(int j = 0, z = 0; z < input.length(); j++, z++) {
            if(s[j] == '?') {
                if(!(found(input[z], good_chars))) {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] == '*') {
                if(j == s.length() - 1) {
                    if(found_str(input.substr(z, input.length()), good_chars)) {
                        cout << "NO" << endl;
                        possible = false;
                        break;
                    } else {
                        z = input.length();
                    }
                } else if(!found_str(input.substr(z, ((input.length() - (s.length() - (j + 1))) - z)), good_chars)) {
                    z = (input.length() - (s.length() - (j + 1)));
                } else {
                    cout << "NO" << endl;
                    possible = false;
                    break;
                }
            } else if(s[j] != input[z]) {
                cout << "NO" << endl;
                possible = false;
                break;
            }
        }

        if(possible) {
            cout << "YES" << endl;
        }
    }
    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else if (s1[j] == '*') {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        i ++, j ++;
                        continue;
                      }
                      if (a > b){
                          if (a-b == 1) j ++;
                          else {
                            f = 0; break;
                          }
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
              }
              else {
                f = 0; break;
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j ++;
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else if (s1[j] == '*') {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        i ++, j ++;
                        continue;
                      }
                      if (a > b){
                          f = 0; break;
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
              }
              else {
                f = 0; break;
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j = s1.size();
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && i < s1.size(); i ++){
              if (x[i] == s1[i]) continue;
              int p = x[i]-'a';
              if (s1[i] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                //cout << a1[p] << " \n";
              }
              else {
                  if (a1[p]){
                    f = 0; break;
                  }
              }
        }
        if (f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1;
        for (int i = 0; i < x.size() && i < s1.size(); i ++){
              if (x[i] == s1[i]) continue;
              int p = x[i]-'a';
              if (s1[i] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                //cout << a1[p] << " \n";
              }
              else {
                  if (b1[p] == 0){
                    f = 0; break;
                  }
              }
        }
        if (f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else {
                  if (j == s1.size()-1){
                     for ( ; i < x.size(); i ++){
                          if (a1[x[i]-'a']){
                            f = 0; break;
                          }
                     }
                     j ++;
                  }
                  else {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a <= b){
                          if (a1[p]){
                             f = 0; break;
                          }
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i < d+c; i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
                  }
              }
              if (f == 0) break;
        }
        if (s1[j] == '*') j ++;
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else if (s1[j] == '*') {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        i ++, j ++;
                        continue;
                      }
                      if (a > b){
                          f = 0; break;
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
              }
              else {
                f = 0; break;
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j ++;
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else {
                  if (j == s1.size()-1){
                     for ( ; i < x.size(); i ++){
                          if (a1[x[i]-'a']){
                            f = 0; break;
                          }
                     }
                  }
                  else {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a <= b){
                          if (a1[p]){
                             f = 0; break;
                          }
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i < d+c; i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                      }
                  }
              }
              if (f == 0) break;
        }
        if (s1[j] == '*') j ++;
        if (i == x.size() && j == s1.size()) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else {
                  if (j == s1.size()-1){
                     for ( ; i < x.size(); i ++){
                          if (a1[x[i]-'a']){
                            f = 0; break;
                          }
                     }
                     j ++;
                     f1 = 1;
                  }
                  else {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        i ++, j ++;
                        continue;
                      }
                      if (a > b){
                          f = 0; break;
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
                  }
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j = s1.size();
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        i ++, j ++;
                        continue;
                      }
                      if (a > b){
                          f = 0; break;
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j ++;
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


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

typedef long long int ll;
const int N = 1000010;

string s,s1;
ll n,q;
ll a1[30],b1[30];

int main()
{
    cin >> s >> s1;
    for (int i = 0; i < s.size(); i ++) a1[s[i]-'a'] ++;
    for (int i = 0; i < 26; i ++){
        if (a1[i] == 0) b1[i] ++;
    }
    cin >> q;
    while (q--){
        string x; cin >> x;
        bool f = 1,f1 = 0;
        int i = 0, j = 0;
        for (i = 0; i < x.size() && j < s1.size(); ){
              if (x[i] == s1[j]){
                i ++ , j ++; continue;
              }
              int p = x[i]-'a';
              if (s1[j] == '?'){
                if (a1[p] == 0){
                    f = 0; break;
                }
                i ++ , j ++;
                //cout << a1[p] << " \n";
              }
              else {
                  if (j == s1.size()-1){
                     for ( ; i < x.size(); i ++){
                          if (a1[x[i]-'a']){
                            f = 0; break;
                          }
                     }
                     j ++;
                     f1 = 1;
                  }
                  else {
                      int a = s1.size()-j;
                      int b = x.size()-i;
                      if (a == b){
                        if (a1[p]){
                            f = 0; break;
                        }
                        continue;
                      }
                      if (a > b){
                          f = 0; break;
                          i ++, j ++;
                      }
                      else {
                         int c = b-a, d = i;
                         for ( ; i <= d+c && i < x.size(); i ++){
                            if (a1[x[i]-'a']){
                                f = 0; break;
                            }
                         }
                         j ++;
                      }
                  }
              }
              if (f == 0) break;
        }
        if (s1[j] == '*' && j < s1.size()) j = s1.size();
        if (i == x.size() && j == s1.size() && f) cout << "YES\n";
        else cout << "NO\n";
    }

    //main();

    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    bool letters[27];
    for(int i = 0;i<26;i++)
    {
        letters[i] = 0;
    }
    cin >> s;

    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a']=1;
    }

    string s2;
    cin >> s2;
    int n;
    cin >> n;
    string s3;
    for(int i = 0;i<n;i++)
    {
        cin >> s3;
        bool b = 1;

    if(s3.length() <= s2.length())
        for(int j = 0;j<s2.length();j++)
        {
            if(s2[j] != '?' && s2[j] != '*')
            {
                if(s2[j] != s3[j])
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }else if(s2[j] == '?')
            {
                if(letters[s3[j]-'a'] == 0)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }
        }else
        {
            b=0;
            cout<< "NO" << "\n";
        }
        if(b)
            cout << "YES" << "\n";

    }
    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    bool letters[27];
    for(int i = 0;i<26;i++)
    {
        letters[i] = 0;
    }
    cin >> s;

    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a']=1;
    }

    string s2;
    cin >> s2;
    int pre=0,posle=0;
    bool b2=0;
    int pos=0;
    for(int i = 0;i<s2.length();i++)
    {
     if(s2[i] == '*')
     {
         pos=i;
        b2=1;
        posle--;
     }

    if(b2 == 0)
        pre++;
    else
        posle++;
    }

    int n;
    cin >> n;
    string s3;
    for(int i = 0;i<n;i++)
    {
        cin >> s3;
        bool b = 1;

        for(int j = 0;j<pre;j++)
        {
            if(s2[j] != '?' && s2[j] != '*')
            {
                if(s2[j] != s3[j])
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }else if(s2[j] == '?')
            {
                if(letters[s3[j]-'a'] == 0)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }
        }
        if(b)
        {
            for(int j = s.length()-1;j>s.length()-1-posle;j--)
            {
                if(s2[j] != '?' && s2[j] != '*')
            {
                if(s2[j] != s3[j])
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }else if(s2[j] == '?')
            {
                if(letters[s3[j]-'a'] == 0)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }

            }
        }

            for(int j = pos;j<pos+s3.length()-pre-posle;j++)
            {
                if(letters[s3[j]-'a'] == 1)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }

        if(b)
            cout << "YES" << "\n";

    }
    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() < ans.length())
            desno=1;
        else
            desno=0;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i != (ans.length() - posle-1);i--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i-desno]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i-desno])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() > ans.length())
            desno=1;
        else
            desno=0;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i != (ans.length() - posle-1);i--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i-desno]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i-desno])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    bool letters[27];
    for(int i = 0;i<26;i++)
    {
        letters[i] = 0;
    }
    cin >> s;

    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a']=1;
    }

    string s2;
    cin >> s2;
    int pre=0,posle=0;
    bool b2=0;
    int pos=0;
    for(int i = 0;i<s2.length();i++)
    {
     if(s2[i] == '*')
     {
         pos=i;
        b2=1;
        posle--;
     }

    if(b2 == 0)
        pre++;
    else
        posle++;
    }

    int n;
    cin >> n;
    string s3;
    for(int i = 0;i<n;i++)
    {
        cin >> s3;
        bool b = 1;

        for(int j = 0;j<pre;j++)
        {
            if(s2[j] != '?' && s2[j] != '*')
            {
                if(s2[j] != s3[j])
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }else if(s2[j] == '?')
            {
                if(letters[s3[j]-'a'] == 0)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }
        }
        if(b)
        {
            for(int j = s.length()-1;j>s.length()-1-posle;j--)
            {
                if(s2[j] != '?' && s2[j] != '*')
            {
                if(s2[j] != s3[j])
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }else if(s2[j] == '?')
            {
                if(letters[s3[j]-'a'] == 0)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }

            }
        }

            for(int j = pos;j<s3.length()-(pre+posle);j++)
            {
                if(letters[s3[j]-'a'] == 1)
                {
                    b=0;
                    cout << "NO" << "\n";
                    break;
                }
            }

        if(b)
            cout << "YES" << "\n";

    }
    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            desno=1;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i != (ans.length() - posle-1);i--)
            {

                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i-desno])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() < ans.length())
            desno=1;
        else
            desno=0;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1, j = pitanje.length()-1;i != (ans.length() - posle-1);i--,j--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[j]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[j])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i>=ans.length() - posle;i--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999)
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() < ans.length())
            desno=1;
        else
            desno=0;

        if(pos == 9999999 && (pitanje.length() > ans.length() || pitanje.length()<ans.length()))
            b=0;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1, j = pitanje.length()-1;i != (ans.length() - posle-1);i--,j--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[j]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[j])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() < ans.length())
            desno=1;
        else
            desno=0;

        if(pos = 9999999 && (pitanje.length() > ans.length() || pitanje.length()<ans.length()))
            b=0;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1, j = pitanje.length()-1;i != (ans.length() - posle-1);i--,j--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[j]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[j])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i != (ans.length() - posle-1);i--)
            {

                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999)
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i>=ans.length() - posle;i--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999)
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }


#include <iostream>
#include <algorithm>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
#include <stdio.h>
#include <string>
#include <stack>
#include <cassert>
#include <fstream>
#include <queue>
#include <sstream>
#include <set>
#include <numeric>

using namespace std;

typedef long long ll;
typedef long double ld;

int main()
{
    std::ios::sync_with_stdio(false);
    string s;
    cin >> s;
    bool letters[27];
    memset(letters,0,sizeof(letters));
    for(int i = 0;i<s.length();i++)
    {
        letters[s[i]-'a'] = 1;
    }
    int desno=0;
    string ans;
    int n;
    cin >> ans;
    cin >> n;
    int pre=0,posle=0;
    for(int i = 0;i<ans.length();i++)
    {
        if(ans[i] == '*')
            break;
        pre++;
    }
    int pos=9999999;
    for(int i = ans.length()-1;i>=0;i--)
    {
        if(ans[i] == '*')
        {
            pos=i;
            desno=1;
            break;
        }
        posle++;
    }

    for(int i = 0;i<n;i++)
    {
        string pitanje;
        cin >> pitanje;
        bool b=1;

        if(pitanje.length() >= ans.length()-1)
        {

            for(int i = 0;i<pre;i++)
            {
                if(ans[i] == '?')
                {
                    if(letters[pitanje[i]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i])
                    {
                        b=0;
                        break;
                    }
                }

            }

            for(int i = ans.length()-1;i != (ans.length() - posle-1);i--)
            {
                 if(ans[i] == '?')
                {
                    if(letters[pitanje[i-desno]-'a'] == 0)
                    {
                        b=0;
                        break;
                    }
                }else
                {
                    if(ans[i] != pitanje[i-desno])
                    {
                        b=0;
                        break;
                    }
                }
            }

            if(pos != 9999999 && pitanje.length() >= ans.length())
            {
                for(int i = pos;i<=pos+ pitanje.length()-ans.length();i++)
                {
                    if(letters[pitanje[i]-'a'] == 1)
                    {
                        b=0;
                        break;
                    }
                }
            }

        }else
        b=0;

        if(b)
            cout << "YES" << "\n";
        else
            cout << "NO" << "\n";
    }

    return 0;
    }