#include <bits/stdc++.h>
bool good[2 * 255];
char goodstr[100];
char pat[4 * 1000000];
int n, aspos = -1, patlen, templen;
char temp[4 * 1000000];
bool isbad(int l, int r) {
  for (int i = l; i < r; ++i) {
    if (good[temp[i]]) return false;
  }
  return true;
}
bool check(int l, int r, int offset) {
  for (int i = l; i < r; ++i) {
    if (pat[i] == '?') {
      if (!good[temp[i + offset]]) return false;
    } else {
      if (temp[i + offset] != pat[i]) return false;
    }
  }
  return true;
}
int main() {
  scanf(" %s", goodstr);
  scanf(" %s", pat);
  scanf("%d", &n);
  patlen = strlen(pat);
  for (int i = 0; i < patlen; ++i) {
    if (pat[i] == '*') {
      aspos = i;
      break;
    }
  }
  for (int i = 0; i < strlen(goodstr); ++i) {
    good[goodstr[i]] = true;
  }
  for (int i = 0; i < n; ++i) {
    scanf(" %s", temp);
    templen = strlen(temp);
    if (aspos != -1) {
      if (templen + 1 < patlen or !check(0, aspos, 0) or
          !check(aspos + 1, patlen, templen - patlen) or
          !isbad(aspos, aspos + templen - patlen + 1)) {
        printf("NO\n");
      } else {
        printf("YES\n");
      }
    } else {
      if (patlen != templen or !check(0, patlen, 0))
        printf("NO\n");
      else
        printf("YES\n");
    }
  }
  return 0;
}
