#include <bits/stdc++.h>
using namespace std;
void scan(vector<int>& v) {
  for (int i = 0; i < v.size(); i++) cin >> v[i];
}
void print(vector<long long>& v) {
  for (long long i = 0; i < v.size(); i++) cout << v[i];
}
void fun() {
  vector<int> vec(26, 0);
  string second;
  cin >> second;
  for (int i = 0; i < second.length(); i++) {
    vec[second[i] - 'a']++;
  }
  string str;
  cin >> str;
  int len = str.length();
  bool containstar = 0;
  int pos = -1;
  for (int i = 0; i < str.length(); i++) {
    if (str[i] == '*') {
      containstar = true;
      pos = i;
      break;
    }
  }
  string q = str.substr(0, pos);
  string e = str.substr(pos + 1);
  string com;
  int temp;
  cin >> temp;
  while (temp--) {
    string pat;
    cin >> pat;
    int l = pat.length();
    if (!containstar) {
      if (l != len) {
        cout << "NO" << endl;
        continue;
      }
      com = str;
    } else {
      int n = l - len + 1;
      if (n < 0) {
        cout << "NO" << endl;
        continue;
      }
      string w;
      while (n--) w += '!';
      com = (q + w) + e;
    }
    bool same = true;
    for (int i = 0; i < l; i++) {
      if (com[i] != '!' && com[i] != '?') {
        if (com[i] != pat[i]) {
          same = false;
          break;
        }
      } else if (com[i] == '?') {
        if (!vec[pat[i] - 'a']) {
          same = false;
          break;
        }
      } else {
        if (vec[pat[i] - 'a']) {
          same = false;
          break;
        }
      }
    }
    if (same)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
}
int main() {
  fun();
  long long acc;
  cin >> acc;
  return 0;
}
