#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
char s[maxn], t[maxn], ch[maxn];
bool is_good[1000];
int n, len;
int find_index() {
  for (int i = 0; i < len; i++) {
    if (s[i] == '*') return i;
  }
  return -1;
}
bool check(int index) {
  int lent = strlen(t);
  int mid = index;
  if (lent < len + (index != -1) * (-1)) return 0;
  if (lent > len && index == -1) return 0;
  if (index == -1) mid = len;
  for (int i = 0; i < mid; i++) {
    if (s[i] != t[i] && (s[i] != '?' || !is_good[t[i]])) return 0;
  }
  int tp = strlen(t) - 1;
  for (int i = len - 1; i > mid; i--) {
    if (s[i] != t[tp] && (s[i] != '?' || !is_good[t[tp]])) {
      return 0;
    } else
      tp--;
  }
  for (int i = mid; i <= tp; i++) {
    if (is_good[t[i]]) return 0;
  }
  return 1;
}
int main() {
  scanf("%s", ch);
  len = strlen(ch);
  for (int i = 0; i < len; i++) is_good[ch[i]] = 1;
  scanf("%s", s);
  len = strlen(s);
  scanf("%d", &n);
  int is = find_index();
  while (n--) {
    scanf("%s", t);
    if (check(is)) {
      printf("YES\n");
    } else
      printf("NO\n");
  }
}
