#include <bits/stdc++.h>
using namespace std;
int main(int argc, const char **argv) {
  if (argc >= 2) {
  }
  string g, s;
  cin >> g >> s;
  map<char, int> good, bad;
  for (auto ch : g) {
    good[ch] = 1;
  }
  for (char i = 'a'; i <= 'z'; i++) {
    if (good[i] == 0) {
      bad[i] = 1;
    }
  }
  string sf, sl;
  sf = sl = "";
  int f = 0;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '*') {
      f = 1;
      continue;
    }
    if (f) {
      sl += s[i];
    } else {
      sf += s[i];
    }
  }
  int q;
  cin >> q;
  while (q--) {
    string t;
    cin >> t;
    int l = 0;
    int r = t.length() - 1;
    int pos = 1;
    if (t.length() < sf.length() + sl.length()) {
      cout << "NO" << endl;
      continue;
    }
    for (int i = 0; i < sf.length(); i++, l++) {
      if (sf[i] == t[i] || (sf[i] == '?' && good[t[i]])) {
        continue;
      }
      pos = 0;
      break;
    }
    for (int i = 0; i < sl.length(); i++, r--) {
      if (sl[sl.length() - i - 1] == t[t.length() - i - 1] ||
          (sl[sl.length() - i - 1] == '?' && good[t[t.length() - i - 1]])) {
        continue;
      }
      pos = 0;
      break;
    }
    if (!pos) {
      cout << "NO" << endl;
      continue;
    }
    if (r >= l) {
      pos = f;
      for (int i = l; pos && i <= r; i++) {
        if (bad[t[i]]) {
          continue;
        }
        pos = 0;
      }
    }
    if (pos) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}
