#include <bits/stdc++.h>
using namespace std;
char good[200];
bool Ok(string pat, string t) {
  if (pat.length() != t.length()) return false;
  bool ok = 1;
  for (int i = 0; i < t.length() and ok; i++) {
    if (pat[i] == '?')
      ok &= good[t[i]];
    else
      ok &= t[i] == pat[i];
  }
  return ok;
}
int main() {
  string pat;
  string fst, sec;
  string goo;
  string t;
  cin >> goo;
  for (auto x : goo) good[x] = 1;
  cin >> pat;
  bool ok = 0;
  int pos = pat.find('*');
  if (pos != -1) {
    for (int i = 0; i < pos; i++) {
      fst += pat[i];
    }
    for (int i = pos + 1; i < pat.length(); i++) {
      sec += pat[i];
    }
  }
  int m;
  cin >> m;
  while (m--) {
    cin >> t;
    bool ok = 1;
    string mid, fstt, sect;
    if (pos == -1) {
      ok = Ok(pat, t);
    } else {
      for (int i = 0; i < min((int)t.length(), pos); i++) {
        fstt += t[i];
      }
      for (int i = pos; i < (int)t.length() - ((int)pat.length() - pos) + 1;
           i++)
        mid += t[i];
      for (int i = max((int)t.length() - ((int)pat.length() - pos) + 1, pos);
           i < (int)t.length(); i++)
        sect += t[i];
      for (auto x : mid) ok &= !good[x];
      ok &= Ok(fst, fstt) & Ok(sec, sect);
    }
    if (ok) {
      cout << "YES" << endl;
    } else
      cout << "NO" << endl;
  }
  return 0;
}
