#include <bits/stdc++.h>
const int mod = 10000009;
const long long int MOD = 1000000007;
int DEBUG = 0;
using namespace std;
long long int powermod(long long int _a, long long int _b,
                       long long int _m = MOD) {
  long long int _r = 1;
  while (_b) {
    if (_b % 2 == 1) _r = (_r * _a) % _m;
    _b /= 2;
    _a = (_a * _a) % _m;
  }
  return _r;
}
long long int add(long long int a, long long int b, long long int m = MOD) {
  long long int x = a + b;
  while (x >= m) x -= m;
  return x;
}
long long int sub(long long int a, long long int b, long long int m = MOD) {
  long long int x = a - b;
  while (x < 0) x += m;
  return x;
}
long long int mul(long long int a, long long int b, long long int m = MOD) {
  long long int x = a * 1ll * b;
  x %= m;
  if (x < 0) x += m;
  return x;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  string str;
  cin >> str;
  bool good[150];
  memset(good, false, 150);
  for (int i = 0; i < str.length(); i++) good[str[i]] = true;
  string pat;
  cin >> pat;
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if (s.length() < pat.length() - 1) {
      printf("NO\n");
      continue;
    }
    bool yes = true;
    bool star = false;
    for (int i = 0; i < pat.length(); i++) {
      if (pat[i] == '*') {
        star = true;
        break;
      } else if (pat[i] == '?' && !good[s[i]]) {
        yes = false;
        break;
      } else if (pat[i] != '*' && pat[i] != '?' && pat[i] != s[i]) {
        yes = false;
        break;
      }
    }
    if (!star && s.length() != pat.length()) {
      printf("NO\n");
      continue;
    }
    if (!yes) {
      printf("NO\n");
      continue;
    }
    if (star) {
      int L1 = pat.length() - 1;
      int L2 = s.length() - 1;
      int i = 0;
      for (i = L1; pat[i] != '*'; i--) {
        if (pat[i] == '?' && !good[s[L2 - (L1 - i)]]) {
          yes = false;
          break;
        } else if (pat[i] != '*' && pat[i] != '?' &&
                   pat[i] != s[L2 - (L1 - i)]) {
          yes = false;
          break;
        }
      }
      if (!yes) {
        printf("NO\n");
        continue;
      }
      int start = i;
      int end = (L2 - (L1 - i));
      for (int j = start; j <= end; j++)
        if (good[s[j]]) {
          yes = false;
          break;
        }
    }
    if (yes)
      printf("YES");
    else
      printf("NO");
    printf("\n");
  }
  return 0;
}
