#include <bits/stdc++.h>
using namespace std;
bool cnt[26];
bool check(string s, string t) {
  int i, j, lens = s.size(), lent = t.size();
  if (lens != lent) return 0;
  for (i = 0; i < lens; i++) {
    if (s[i] == '?' && !cnt[t[i] - 'a'])
      return 0;
    else if (s[i] != '?' && s[i] != t[i])
      return 0;
  }
  return 1;
}
int main() {
  int l, i, j, n, k;
  string g, s, t, x;
  cin >> g >> s;
  l = g.size();
  int lens = s.size();
  for (i = 0; i < l; i++) cnt[g[i] - 'a'] = 1;
  auto f = s.find('*');
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> t;
    int lent = t.size();
    if (lent < lens - 1)
      cout << "NO" << endl;
    else if (f == string::npos) {
      if (check(s, t))
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else {
      bool tr = 1;
      tr &= check(s.substr(0, f), t.substr(0, f));
      reverse(t.begin(), t.end());
      reverse(s.begin(), s.end());
      f = lens - f - 1;
      tr &= check(s.substr(0, f), t.substr(0, f));
      reverse(t.begin(), t.end());
      reverse(s.begin(), s.end());
      f = lens - f - 1;
      for (j = f; j < lent - (lens - f - 1); j++) tr &= !cnt[t[j] - 'a'];
      cout << (tr ? "YES" : "NO") << endl;
    }
  }
  return 0;
}
