#include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7;
const int maxn = 1e6 + 10;
long long n, k;
string s1, s2, s3;
int H[1000];
int main() {
  memset(H, 0, sizeof(H));
  cin >> s1 >> s2;
  for (int i = 0; i < s1.size(); i++) H[s1[i]] = 1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s3;
    int l = s3.size() - s2.size() + 1;
    int i1 = 0, i2 = 0;
    if (l < 0) goto bd;
    if (s2.find('*') == string::npos && s2.size() != s3.size()) goto bd;
    while (i1 < s2.size() && i2 < s3.size()) {
      if (s2[i1] == '*') {
        for (int k = 0; k < l; k++)
          if (H[s3[i2++]] != 0) goto bd;
      } else if (s2[i1] == '?') {
        if (H[s3[i2++]] != 1) goto bd;
      } else {
        if (s3[i2++] != s2[i1]) goto bd;
      }
      i1++;
    }
    puts("YES");
    continue;
  bd:
    puts("NO");
  }
  return 0;
}
