//#ezcode
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define F first
#define S second
#define PB push_back
#define MP make_pair
/*long long bin_pow(long long x, long long e) {
long long k;
if (e == 0) return 1;
if (e % 2) return bin_pow(x, e - 1) * x;
else {
k = bin_pow(x, e / 2);
return k * k;
}
}*/
string g;
string s;
int n;
string k;
int t, z, r = 0;
int testy = 0;
int tests = 0;
int testx = 0;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
//freopen("in.txt", "r", stdin);
//freopen("out.txt", "w", stdout);
cin >> g >> s >> n;
t = s.length();
for (int j = 0; j < n; j++) {
testx = 0;
testy = 0;
cin >> k;
z = k.length();
if (t - z >= 2) {
cout << "NO" << '\n';
continue;
}
r = 0;
for (int i = 0; i < t; i++) {
if (s[i] == '?') {
for (auto q : g) {
if (q == k[r]) {
testy++;
break;
}
}
if (testy == 0) {
cout << "NO" << '\n';
testx++;
break;
}
}
else if (s[i] == '*') {
for (int e = r; e < r + z - t + 1; e++) {
for (auto q : g) {
if (q == k[e]) {
cout << "NO" << '\n';
testx++;
break;
}
}
if (testx) break;
}
r += z - t;
if (testx) break;
}
else if (s[i] != k[r]) {
cout << "NO" << '\n';
testx++;
break;
}
r++;
}
if (testx == 0) cout << "YES" << '\n';
}
return 0;
}
Ly8jZXpjb2RlCgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgp0eXBlZGVmIGxvbmcgbG9uZyBsbDsKdHlwZWRlZiB2ZWN0b3I8aW50PiB2aTsKdHlwZWRlZiBwYWlyPGludCxpbnQ+IHBpaTsKCiNkZWZpbmUgRiBmaXJzdAojZGVmaW5lIFMgc2Vjb25kCiNkZWZpbmUgUEIgcHVzaF9iYWNrCiNkZWZpbmUgTVAgbWFrZV9wYWlyCgovKmxvbmcgbG9uZyBiaW5fcG93KGxvbmcgbG9uZyB4LCBsb25nIGxvbmcgZSkgewogICAgbG9uZyBsb25nIGs7CiAgICBpZiAoZSA9PSAwKSByZXR1cm4gMTsKICAgIGlmIChlICUgMikgcmV0dXJuIGJpbl9wb3coeCwgZSAtIDEpICogeDsKICAgIGVsc2UgewogICAgICAgIGsgPSBiaW5fcG93KHgsIGUgLyAyKTsKICAgICAgICByZXR1cm4gayAqIGs7CiAgICB9Cn0qLwoKc3RyaW5nIGc7CnN0cmluZyBzOwppbnQgbjsKc3RyaW5nIGs7CmludCB0LCB6LCByID0gMDsKaW50IHRlc3R5ID0gMDsKaW50IHRlc3RzID0gMDsKaW50IHRlc3R4ID0gMDsKCmludCBtYWluKCkgewogICAgaW9zX2Jhc2U6OnN5bmNfd2l0aF9zdGRpbygwKTsgY2luLnRpZSgwKTsKICAgIC8vZnJlb3BlbigiaW4udHh0IiwgInIiLCBzdGRpbik7CiAgICAvL2ZyZW9wZW4oIm91dC50eHQiLCAidyIsIHN0ZG91dCk7CiAgICBjaW4gPj4gZyA+PiBzID4+IG47CiAgICB0ID0gcy5sZW5ndGgoKTsKICAgIGZvciAoaW50IGogPSAwOyBqIDwgbjsgaisrKSB7CiAgICAgICAgdGVzdHggPSAwOwogICAgICAgIHRlc3R5ID0gMDsKICAgICAgICBjaW4gPj4gazsKICAgICAgICB6ID0gay5sZW5ndGgoKTsKICAgICAgICBpZiAodCAtIHogPj0gMikgewogICAgICAgICAgICBjb3V0IDw8ICJOTyIgPDwgJ1xuJzsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIHIgPSAwOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgdDsgaSsrKSB7CiAgICAgICAgICAgIGlmIChzW2ldID09ICc/JykgewogICAgICAgICAgICAgICAgZm9yIChhdXRvIHEgOiBnKSB7CiAgICAgICAgICAgICAgICAgICAgaWYgKHEgPT0ga1tyXSkgewogICAgICAgICAgICAgICAgICAgICAgICB0ZXN0eSsrOwogICAgICAgICAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICBpZiAodGVzdHkgPT0gMCkgewogICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgIk5PIiA8PCAnXG4nOwogICAgICAgICAgICAgICAgICAgIHRlc3R4Kys7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZWxzZSBpZiAoc1tpXSA9PSAnKicpIHsKICAgICAgICAgICAgICAgIGZvciAoaW50IGUgPSByOyBlIDwgciArIHogLSB0ICsgMTsgZSsrKSB7CiAgICAgICAgICAgICAgICAgICAgZm9yIChhdXRvIHEgOiBnKSB7CiAgICAgICAgICAgICAgICAgICAgICAgIGlmIChxID09IGtbZV0pIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGNvdXQgPDwgIk5PIiA8PCAnXG4nOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgdGVzdHgrKzsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgIGlmICh0ZXN0eCkgYnJlYWs7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICByICs9IHogLSB0OwogICAgICAgICAgICAgICAgaWYgKHRlc3R4KSBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBlbHNlIGlmIChzW2ldICE9IGtbcl0pIHsKICAgICAgICAgICAgICAgIGNvdXQgPDwgIk5PIiA8PCAnXG4nOwogICAgICAgICAgICAgICAgdGVzdHgrKzsKICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICAgICAgICAgIHIrKzsKICAgICAgICB9CiAgICAgICAgaWYgKHRlc3R4ID09IDApIGNvdXQgPDwgIllFUyIgPDwgJ1xuJzsKICAgIH0KICAgIHJldHVybiAwOwp9