#include <bits/stdc++.h>
using namespace std;
int main() {
  map<char, int> good;
  string gd;
  cin >> gd;
  int d = 0;
  for (int i = 0; i < gd.size(); i++) {
    good[gd[i]] = 1;
  }
  string s, s1;
  cin >> s;
  int l = -1, est = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '*') {
      l = i;
      est = 1;
    } else {
      s1 += s[i];
    }
  }
  int q;
  cin >> q;
  for (int op = 0; op < q; op++) {
    string z, z1;
    cin >> z;
    if (est == 1) {
      if (z.size() < s.size() - 1) {
        cout << "NO" << endl;
        goto momo;
      } else {
        if (z.size() == s.size() - 1) {
          for (int i = 0; i < z.size(); i++) {
            if (s1[i] == '?') {
              if (good[z[i]] != 1) {
                cout << "NO" << endl;
                goto momo;
              }
            } else {
              if (s1[i] != z[i]) {
                cout << "NO" << endl;
                goto momo;
              }
            }
          }
        } else {
          int kol = z.size() - s.size();
          for (int i = 0; i < z.size(); i++) {
            if (i >= l && i <= l + kol) {
            } else {
              z1 += z[i];
            }
          }
          for (int i = l; i <= l + kol; i++) {
            if (good[z[i]] == 1) {
              cout << "NO" << endl;
              goto momo;
            }
          }
          for (int i = 0; i < z1.size(); i++) {
            if (s1[i] == '?') {
              if (good[z1[i]] == 0) {
                cout << "NO" << endl;
                goto momo;
              }
            } else {
              if (s1[i] != z1[i]) {
                cout << "NO" << endl;
                goto momo;
              }
            }
          }
        }
      }
    } else {
      if (z.size() != s.size()) {
        cout << "NO" << endl;
        goto momo;
      } else {
        for (int i = 0; i < z.size(); i++) {
          if (s[i] == '?') {
            if (good[z[i]] != 1) {
              cout << "NO" << endl;
              goto momo;
            }
          } else {
            if (z[i] != s[i]) {
              cout << "NO" << endl;
              goto momo;
            }
          }
        }
      }
    }
    cout << "YES" << endl;
  momo:;
  }
  return 0;
}
