#include <bits/stdc++.h>
using namespace std;
char inc() {
  char _[10];
  scanf("%s", _);
  return _[0];
}
int ini() {
  int _;
  scanf("%d", &_);
  return _;
}
unsigned inu() {
  unsigned _;
  scanf("%u", &_);
  return _;
}
long long inll() {
  long long _;
  scanf("%lld", &_);
  return _;
}
double ind() {
  double _;
  scanf("%lf", &_);
  return _;
}
string ins() {
  string _;
  cin >> _;
  return _;
}
int inl(char _[]) {
  if (!fgets(_, (int)1e8, stdin)) return -1;
  int i = strlen(_);
  if (_[i - 1] == '\n') _[--i] = 0;
  return i;
}
const int inf = 0x3f3f3f3f;
const long long lnf = 0x3f3f3f3f3f3f3f3fLL;
const double pi = 3.14159265358979323846;
const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 1e5 + 10;
int n;
char gd[30], a[maxn], b[maxn];
set<char> good;
int main() {
  int CAS = 0;
  while (scanf("%s", gd) != EOF) {
    good.clear();
    for (int i = 0; gd[i]; ++i) {
      good.insert(gd[i]);
    }
    scanf("%s%d", a, &n);
    int lena = strlen(a);
    while (n--) {
      scanf("%s", b);
      int lenb = strlen(b);
      int i = 0, j = 0;
      for (; j < lenb && i < lena;) {
        if (a[i] == b[j]) {
          ++i;
          ++j;
        } else if (a[i] == '?') {
          if (good.find(b[j]) != good.end()) {
            ++i;
            ++j;
          } else {
            break;
          }
        } else if (a[i] == '*') {
          if (lena - i > lenb - j) {
            ++i;
          } else if (lena - i <= lenb - j && good.find(b[j]) == good.end()) {
            if (lena - i == lenb - j) ++i;
            ++j;
          } else {
            break;
          }
        } else {
          break;
        }
      }
      if (i == lena - 1 && a[i] == '*') i = lena;
      puts(i == lena && j == lenb ? "YES" : "NO");
    }
  }
  return 0;
}
