#include <bits/stdc++.h>
using namespace std;
long long m = 1e9 + 7;
double eps = 1e-6;
bool good[256];
long long n, spos = -1;
int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  string g, pat;
  cin >> g;
  cin >> pat;
  cin >> n;
  for (int i = 0; (i) < (256); ++i) good[i] = false;
  for (int i = 0; (i) < (((int)(pat).size())); ++i)
    if (pat[i] == '*') spos = i;
  for (int i = 0; (i) < (((int)(g).size())); ++i) {
    good[int(g[i])] = true;
  }
  for (int i = 0; (i) < (n); ++i) {
    string s;
    bool pass = true;
    cin >> s;
    if (spos == -1) {
      if (((int)(pat).size()) != ((int)(s).size()))
        cout << "NO" << endl;
      else {
        for (int j = 0; (j) < (((int)(pat).size())); ++j) {
          if (pat[j] != '?' && pat[j] != s[j])
            pass = false;
          else if (pat[j] == '?' && !good[int(s[j])])
            pass = false;
        }
        if (pass)
          cout << "YES" << endl;
        else
          cout << "NO" << endl;
      }
    } else {
      if (((int)(s).size()) - ((int)(pat).size()) < -1)
        cout << "NO" << endl;
      else {
        long long offset = ((int)(s).size()) - ((int)(pat).size());
        for (int j = 0; (j) < (spos); ++j) {
          if (pat[j] != '?' && pat[j] != s[j])
            pass = false;
          else if (pat[j] == '?' && !good[int(s[j])])
            pass = false;
        }
        for (long long j = spos + 1 + offset; j < ((int)(s).size()); j++) {
          if (pat[j - offset] != '?' && pat[j - offset] != s[j])
            pass = false;
          else if (pat[j - offset] == '?' && !good[int(s[j])])
            pass = false;
        }
        for (long long j = spos; j <= spos + offset; j++)
          if (good[int(s[j])]) pass = false;
        if (pass)
          cout << "YES" << endl;
        else
          cout << "NO" << endl;
      }
    }
  }
  return 0;
}
