#include <bits/stdc++.h>
using namespace std;
int n, gs, bs;
string g, b, x;
map<char, int> a;
int main() {
  cin >> g;
  cin >> b;
  cin >> n;
  gs = g.size();
  bs = b.size();
  for (int i = 0; i < gs; i++) a[g[i]] = 1;
  bool u = 0;
  for (int j = 0; j < bs; j++) {
    if (b[j] == '*') u = 1;
  }
  for (int i = 1; i <= n; i++) {
    cin >> x;
    int t = 0;
    bool h = 0;
    if (x.size() < bs && u == 0) {
      cout << "NO" << endl;
      continue;
    }
    if (x.size() < bs - 1 && u == 1) {
      cout << "NO" << endl;
      continue;
    }
    for (int j = 0; j < x.size(); j++) {
      if (b[t] != x[j] && b[t] != '?' && b[t] != '*') {
        cout << "NO" << endl;
        h = 1;
        break;
      } else if (b[t] == '?' && a[x[j]] == 0) {
        cout << "NO" << endl;
        h = 1;
        break;
      } else if (b[t] == '*' && a[x[j]] == 0 && x.size() - j + 1 > bs - t + 1) {
        t--;
      } else if (b[t] == '*' && (bs - t - 1) - (x.size() - j - 1) == 1) {
        j--;
      } else if (b[t] == '*' && a[x[j]] == 1) {
        cout << "NO" << endl;
        h = 1;
        break;
      }
      t++;
    }
    if (h == 0) cout << "YES" << endl;
  }
  return 0;
}
