#include <bits/stdc++.h>
using namespace std;
string a, b, c, d, e;
bool good[26];
int main() {
  cin >> a;
  for (auto& it : a) good[it - 'a'] = 1;
  cin >> b;
  int n;
  cin >> n;
  int FLAG = 0;
  for (int i = 0; i < b.length(); i++)
    if (b[i] == '*') FLAG = i + 1;
  while (n--) {
    cin >> e;
    if (FLAG == 0) {
      if (b.length() != e.length()) {
        cout << "NO" << endl;
        continue;
      }
      for (int i = 0; i < b.length(); i++)
        if (b[i] != e[i] && (b[i] != '?' || good[e[i] - 'a'] == 0)) {
          cout << "NO" << endl;
          goto LABEL;
        }
      cout << "YES" << endl;
    } else {
      if (b.length() > e.length() + 1) {
        cout << "NO" << endl;
        continue;
      }
      for (int i = 0; i < FLAG - 1; i++)
        if (b[i] != e[i] && (b[i] != '?' || good[e[i] - 'a'] == 0)) {
          cout << "NO" << endl;
          goto LABEL;
        }
      int L1 = FLAG, L2 = FLAG - b.length() + e.length(),
          L3 = b.length() - FLAG;
      for (int i = 0; i < L3; i++)
        if (b[L1 + i] != e[L2 + i] &&
            (b[L1 + i] != '?' || good[e[L2 + i] - 'a'] == 0)) {
          cout << "NO" << endl;
          goto LABEL;
        }
      for (int i = FLAG - 1; i < L2; i++)
        if (good[e[i] - 'a'] == 1) {
          cout << "NO" << endl;
          goto LABEL;
        }
      cout << "YES" << endl;
    }
  LABEL:;
  }
  return 0;
}
