#include <bits/stdc++.h>
using namespace std;
bool check(const string &s, const string &temp, const vector<int> &good,
           vector<bool> &pr) {
  bool ok = true;
  for (int i = 0; i < (int)temp.size(); i++) {
    if (temp[i] == '*') break;
    pr[i] = true;
    if (s[i] != temp[i]) {
      if (temp[i] != '?') {
        ok = false;
      } else if (good[s[i] - 'a'] == 0) {
        ok = false;
      }
    }
  }
  return ok;
}
int main() {
  string s1;
  string temp;
  vector<int> good(26, 0);
  cin >> s1;
  for (char i : s1) {
    good[i - 'a'] = 1;
  }
  cin >> temp;
  bool x = false;
  for (char i : temp) {
    if (i == '*') {
      x = true;
    }
  }
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    if ((!x && s.size() != temp.size()) || (x && temp.size() - 1 > s.size())) {
      cout << "NO\n";
      continue;
    }
    vector<bool> pr(s.size(), false);
    bool ch1 = false, ch2 = false;
    ch1 = check(s, temp, good, pr);
    if (!x) {
      cout << (ch1 ? "YES\n" : "NO\n");
    } else {
      reverse(s.begin(), s.end());
      reverse(temp.begin(), temp.end());
      reverse(pr.begin(), pr.end());
      ch2 = check(s, temp, good, pr);
      if (ch1 && ch2) {
        bool ch3 = true;
        for (int i = 0; i < (int)pr.size(); i++) {
          if (!pr[i] && good[s[i] - 'a'] == 1) {
            ch3 = false;
          }
        }
        cout << (ch3 ? "YES\n" : "NO\n");
      } else {
        cout << "NO\n";
      }
      reverse(temp.begin(), temp.end());
    }
  }
  return 0;
}
