#include <bits/stdc++.h>
using namespace std;
char good[30];
char P[100010], Q[100010];
int main() {
  int msk = 0;
  scanf("%s", good);
  int g = strlen(good);
  for (int i = 0; i < g; i++) msk |= (1 << (good[i] - 'a'));
  scanf("%s", P);
  int n = strlen(P);
  int q;
  scanf("%d", &q);
  int ast = -1;
  for (int i = 0; i < n; i++) {
    if (P[i] == '*') {
      ast = i;
      break;
    }
  }
  while (q--) {
    scanf("%s", Q);
    int m = strlen(Q);
    if (ast == -1) {
      if (m != n)
        printf("NO\n");
      else {
        bool ok = true;
        for (int i = 0; i < n && ok; i++) {
          if (P[i] == '?') {
            ok = ok && (msk & (1 << (Q[i] - 'a')));
          } else
            ok = ok && P[i] == Q[i];
        }
        printf("%s\n", ok ? "YES" : "NO");
      }
    } else {
      if (m < n - 1)
        printf("NO\n");
      else {
        bool ok = true;
        for (int i = 0; i < ast && ok; i++) {
          if (P[i] == '?')
            ok = ok && (msk & (1 << (Q[i] - 'a')));
          else
            ok = ok && P[i] == Q[i];
        }
        int j = m - 1;
        for (int i = n - 1; i > ast && ok; i--, j--) {
          if (P[i] == '?')
            ok = ok && (msk & (1 << Q[j] - 'a'));
          else
            ok = ok && P[i] == Q[j];
        }
        for (int i = ast; i <= j && ok; i++)
          ok = ok && !(msk & (1 << Q[i] - 'a'));
        printf("%s\n", ok ? "YES" : "NO");
      }
    }
  }
  return 0;
}
