#include <bits/stdc++.h>
using namespace std;
int good[27];
int main() {
  string s, t;
  cin >> s;
  for (int i = 0; i < s.length(); i++) {
    good[s[i] - 'a'] = 1;
  }
  int cont = 0;
  cin >> t;
  for (int i = 0; i < t.length(); i++) {
    if (t[i] == '*') {
      cont = 1;
      break;
    }
  }
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    string a;
    cin >> a;
    int dif = a.length() - t.length();
    if (dif < (-1)) {
      printf("NO\n");
      continue;
    }
    if (dif != 0 && !cont) {
      printf("NO\n");
      continue;
    }
    int ptr1 = 0;
    int flg = 0;
    for (int j = 0; j < t.length(); j++) {
      if (t[j] == '?') {
        if (!good[a[ptr1] - 'a']) {
          flg = 1;
          break;
        }
        ptr1++;
      } else if (t[j] == '*') {
        for (int k = 0; k < (dif + 1); k++) {
          if (good[a[ptr1] - 'a']) {
            flg = 1;
          }
          ptr1++;
        }
        if (flg == 1) {
          break;
        }
      } else {
        if (t[j] != a[ptr1]) {
          flg = 1;
          break;
        }
        ptr1++;
      }
      if (flg == 1) {
        break;
      }
    }
    if (ptr1 < a.length()) {
      flg = 1;
    }
    if (flg) {
      printf("NO\n");
    } else {
      printf("YES\n");
    }
  }
  return 0;
}
