#include <bits/stdc++.h>
using namespace std;
bool good[26];
string pat;
int N, star;
bool okay(char p, char w) { return p == w || p == '?' && good[w - 'a']; }
int main() {
  cin >> pat;
  for (int i = 0; i < pat.length(); i++) {
    good[pat[i] - 'a'] = true;
  }
  cin >> pat >> N;
  while (star < pat.length() && pat[star] != '*') star++;
  for (int t = 0; t < N; t++) {
    string word;
    cin >> word;
    bool match = true;
    if (star == pat.length()) {
      if (word.length() != pat.length())
        match = false;
      else
        for (int i = 0; i < pat.length(); i++) match &= okay(pat[i], word[i]);
    } else {
      int d = word.length() - pat.length();
      if (d < -1) {
        match = false;
      } else {
        for (int i = 0; i < star; i++) match &= okay(pat[i], word[i]);
        for (int i = 0; i <= d; i++) match &= !good[word[star + i] - 'a'];
        for (int i = star + 1; i < pat.length(); i++)
          match &= okay(pat[i], word[i + d]);
      }
    }
    cout << (match ? "YES" : "NO") << endl;
  }
}
