#include <bits/stdc++.h>
using namespace std;
int main() {
  string good;
  cin >> good;
  int g[26] = {0};
  for (int i = 0; i < good.length(); i++) g[good[i] - 'a'] = 1;
  string inp;
  cin >> inp;
  int n;
  cin >> n;
  int star = 0;
  for (char i : inp) {
    if (i == '*') {
      star = 1;
    }
  }
  while (n--) {
    string x;
    cin >> x;
    if (!star && x.length() != inp.length()) {
      cout << "NO" << '\n';
    } else if (!star) {
      int i;
      for (i = 0; i < x.length(); i++) {
        if (inp[i] != '?' && x[i] != inp[i]) break;
        if (inp[i] == '?' && !g[x[i] - 'a']) break;
      }
      if (i == x.length())
        cout << "YES";
      else
        cout << "NO";
    } else {
      int i;
      for (i = 0; i < inp.length(); i++) {
        if (inp[i] == '?' && !g[x[i] - 'a']) break;
        if (inp[i] != '?' && inp[i] != '*' && x[i] != inp[i]) break;
        if (inp[i] == '*') {
          int j, k;
          for (j = x.length() - 1, k = inp.length() - 1; j >= i; j--, k--) {
            if (inp[k] == '?' && !g[x[j] - 'a']) break;
            if (inp[k] != '?' && x[j] != inp[k]) break;
          }
          if (k > i)
            break;
          else {
            while (j >= i) {
              if (g[x[j] - 'a']) break;
              j--;
            }
            if (j == i - 1)
              i = inp.length() - 1;
            else
              break;
          }
        }
      }
      if (i == inp.length())
        cout << "YES";
      else
        cout << "NO";
    }
    cout << '\n';
  }
  return 0;
}
