#include <bits/stdc++.h>
using namespace std;
vector<string> v;
int a[26] = {0};
int main() {
  int n, i, j, co = 0, F = -1;
  string s, s2;
  cin >> s;
  for (i = 0; i < s.size(); i++) {
    a[s[i] - 'a'] = 1;
  }
  cin >> s2 >> n;
  while (n--) {
    cin >> s;
    v.push_back(s);
  }
  for (i = 0; i < v.size(); i++) {
    co = 0;
    if (v[i].size() < s2.size() - 1) {
      cout << "NO" << endl;
      continue;
    }
    for (j = 0; j < s2.size(); j++) {
      if (s2[j] == '*') {
        F = j;
        break;
      }
      if ((s2[j] != v[i][j] && s2[j] != '?') ||
          (s2[j] == '?' && a[v[i][j] - 'a'] == 0)) {
        cout << "NO" << endl;
        co = 1;
        break;
      }
    }
    if (co != 1) {
      int K = v[i].size() - 1;
      for (j = s2.size() - 1; j >= 0; j--) {
        if (s2[j] == '*') break;
        if ((s2[j] != v[i][K] && s2[j] != '?') ||
            (s2[j] == '?' && a[v[i][K] - 'a'] == 0)) {
          cout << "NO" << endl;
          co = 1;
          break;
        }
        K--;
      }
    }
    if (co != 1 && v[i].size() >= s2.size() && F != -1) {
      int p = v[i].size() - (s2.size() - 1);
      for (j = F; p != 0; j++) {
        if (a[v[i][j] - 'a'] == 1) {
          cout << "NO" << endl;
          co = 1;
          break;
        }
        p--;
      }
    }
    if (F == -1 && v[i].size() > s2.size() && co != 1) {
      cout << "NO" << endl;
      co = 1;
    }
    if (co == 0) cout << "YES" << endl;
  }
  return 0;
}
