#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
bool good[27];
char s0[MAXN], s1[MAXN], s2[MAXN];
int p;
int n1, n2;
inline bool match();
inline bool match1();
inline bool match2();
inline bool match3();
int main() {
  scanf("%s", s0 + 1);
  for (int i = 1; s0[i] != 0; i++) good[s0[i] - 'a'] = 1;
  scanf("%s", s1 + 1);
  n1 = strlen(s1 + 1);
  p = 0;
  for (int i = 1; i <= n1; i++)
    if (s1[i] == '*') p = i;
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%s", s2 + 1);
    n2 = strlen(s2 + 1);
    bool ans;
    if (p == 0) ans = match();
    if (p == 1) ans = match2();
    if (p == n1) ans = match1();
    if (p > 1 && p < n1) ans = match1() && match2();
    if (p != 0) ans = ans && match3();
    if (ans)
      printf("YES\n");
    else
      printf("NO\n");
  }
  return 0;
}
inline bool match() {
  if (n1 != n2) return 0;
  for (int i = 1; i <= n1; i++) {
    if (s1[i] != '?' && s1[i] != s2[i]) return 0;
    if (s1[i] == '?' && !good[s2[i] - 'a']) return 0;
  }
  return 1;
}
inline bool match1() {
  if (n2 < n1 - 1) return 0;
  for (int i = 1; i < p; i++) {
    if (s1[i] != '?' && s1[i] != s2[i]) return 0;
    if (s1[i] == '?' && !good[s2[i] - 'a']) return 0;
  }
  return 1;
}
inline bool match2() {
  if (n2 < n1 - 1) return 0;
  int p1, p2;
  for (p1 = n1, p2 = n2; p1 > p; p1--, p2--) {
    if (s1[p1] != '?' && s1[p1] != s2[p2]) return 0;
    if (s1[p1] == '?' && !good[s2[p2] - 'a']) return 0;
  }
  return 1;
}
inline bool match3() {
  int l = p, r = n2 - (n1 - p);
  for (int i = l; i <= r; i++)
    if (good[s2[i] - 'a']) return 0;
  return 1;
}
