#include <bits/stdc++.h>
using namespace std;
int main() {
  string s;
  cin >> s;
  set<int> S;
  for (int i = 0; s[i]; i++) S.insert(s[i]);
  cin >> s;
  int l1 = s.length();
  int fa = -1;
  for (int i = 0; s[i]; i++)
    if (s[i] == '*') {
      fa = i;
      s.erase(s.begin() + i);
      l1 -= 1;
      break;
    }
  int n;
  cin >> n;
  while (n--) {
    string c1;
    cin >> c1;
    int f = 0;
    int l2 = c1.length();
    l2 -= l1;
    if (l2 < 0 || l2 > 0 && fa == -1)
      f = 1;
    else {
      if (fa != -1) {
        for (int j = fa; j < fa + l2; j++)
          if (S.count(c1[j])) {
            f = 1;
            break;
          }
        c1.erase(fa, l2);
      }
      if (f == 0) {
        for (int i = 0; s[i]; i++) {
          if (s[i] == '?') {
            if (S.count(c1[i]) == 0) f = 1;
          } else if (s[i] != c1[i])
            f = 1;
          if (f) break;
        }
      }
    }
    printf("%s\n", f == 0 ? "YES" : "NO");
  }
}
