#include <bits/stdc++.h>
using namespace std;
int ispresent(vector<char> v, char a) {
  for (std::vector<char>::iterator ir = v.begin(); ir != v.end(); ir++) {
    if (*ir == a) return 1;
  }
  return 0;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  string goo, s;
  cin >> goo >> s;
  int n;
  cin >> n;
  vector<char> v;
  for (long long i = (long long)1; i <= (long long)goo.length(); i++) {
    v.push_back(goo[i - 1]);
  }
  string cur;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    cin >> cur;
    int j = 0, k = 0;
    int flag = 1;
    while (flag && j < s.length() && k < cur.length()) {
      if (s[j] == '?' && ispresent(v, cur[k])) {
        j++;
        k++;
      } else if (s[j] == '?' && !ispresent(v, cur[k])) {
        flag = 0;
      } else if (s[j] == '*') {
        int temp1 = j + 1, temp2 = k;
        while (temp2 < cur.length() && !ispresent(v, cur[temp2])) {
          temp2++;
        }
        while (temp1 < s.length() && !ispresent(v, s[temp1]) &&
               s[temp1] != '?') {
          temp1++;
        }
        int l = temp1 - 1, m = temp2 - 1;
        while (s[l] == cur[m] && m > k - 1 && l > j - 1) {
          l--;
          m--;
        }
        if (s[l] == '*') {
          j = temp1;
          k = temp2;
        } else {
          flag = 0;
        }
      } else {
        if (s[j] == cur[k]) {
          j++;
          k++;
        } else {
          flag = 0;
        }
      }
    }
    if (j < s.length() && s[j] == '*') j++;
    if (flag == 0) {
      cout << "NO" << endl;
    } else if (j == s.length() && k == cur.length()) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}
