#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
void Solution();
long long int q;
string mstr;
bool isgood[200];
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  Solution();
  return 0;
}
void Solution() {
  string gs;
  cin >> gs;
  for (char c : gs) isgood[c] = 1;
  cin >> mstr;
  cin >> q;
  while (q--) {
    bool is = true;
    bool star = false;
    string query;
    cin >> query;
    long long int r = 0, l = query.length();
    while (r < query.length()) {
      if (mstr[r] == '*') {
        star = true;
        break;
      }
      if (r >= mstr.length()) {
        is = false;
        break;
      }
      if (mstr[r] == '?') {
        if (!isgood[query[r]]) {
          is = false;
          break;
        }
      } else {
        if (query[r] != mstr[r]) {
          is = false;
          break;
        }
      }
      r++;
    }
    for (long long int ic = 0; ic < mstr.length(); ic++) {
      l--;
      long long int i = mstr.length() - ic - 1;
      if (mstr[i] == '*') {
        star = true;
        break;
      }
      if (mstr[i] == '?') {
        if (!isgood[query[l]]) {
          is = false;
          break;
        }
      } else {
        if (query[l] != mstr[i]) {
          is = false;
          break;
        }
      }
    }
    if (star) {
      is = is && (query.length() >= mstr.length() - 1);
    } else {
      is = is && (query.length() == mstr.length());
    }
    if (is && star) {
      for (long long int i = 0; i < query.length() - mstr.length() + 1; i++) {
        if (isgood[query[r]]) {
          is = false;
          break;
        }
        r++;
      }
    }
    if (is) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}
