#include <bits/stdc++.h>
using namespace std;
vector<bool> isGood(255);
bool check(string s, string t) {
  if (s.size() != t.size()) return false;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] != t[i] && (s[i] != '?' || !isGood[t[i]])) return false;
  }
  return true;
}
bool check(string t) {
  for (auto c : t) {
    if (isGood[c]) return false;
  }
  return true;
}
int main(int argc, char *argv[]) {
  string s, s1, s2;
  cin >> s;
  bool flag = false;
  for (auto c : s) {
    isGood[c] = true;
  }
  cin >> s;
  for (auto c : s) {
    if (c == '*') {
      flag = true;
    }
    flag ? s2 += c : s1 += c;
  }
  if (!s2.empty()) s2 = s2.substr(1, s2.size() - 1);
  int q;
  cin >> q;
  while (q--) {
    string t;
    cin >> t;
    if (!flag) {
      cout << (check(s1, t) ? "YES" : "NO") << endl;
    } else {
      if (s.size() - 1 > t.size())
        cout << "NO" << endl;
      else {
        cout << (check(s1, t.substr(0, s1.size())) &&
                         check(t.substr(s1.size(),
                                        t.size() - s2.size() - s1.size())) &&
                         check(s2, t.substr(t.size() - s2.size(), s2.size()))
                     ? "YES"
                     : "NO")
             << endl;
      }
    }
  }
  return 0;
}
