#include <bits/stdc++.h>
using namespace std;
bool status[26];
bool check1(string &pat, string &str) {
  int patSize = pat.size();
  int m = str.size();
  if (patSize != m)
    return false;
  else {
    for (int i = 0; i < patSize; i++) {
      if (pat[i] == '?') {
        if (!status[str[i] - 'a']) return false;
      } else if (pat[i] != str[i])
        return false;
    }
    return true;
  }
}
int main() {
  string good;
  cin >> good;
  for (int i = 0; i < 26; i++) status[i] = false;
  for (int i = 0; i < good.size(); i++) {
    status[good[i] - 'a'] = true;
  }
  string pat;
  cin >> pat;
  int patSize = pat.size();
  int star = -1;
  for (int i = 0; i < patSize; i++) {
    if (pat[i] == '*') star = i;
  }
  int n;
  cin >> n;
  while (n--) {
    string str;
    cin >> str;
    if (star == -1) {
      if (check1(pat, str))
        cout << "YES" << endl;
      else
        cout << "NO" << endl;
    } else {
      int m = str.size();
      int size1 = star;
      int size2 = patSize - (star + 1);
      bool ans1, ans2;
      ans1 = ans2 = false;
      if (m < patSize - 1) {
        cout << "NO" << endl;
        continue;
      }
      if (size1 > 0) {
        string str1 = pat.substr(0, size1);
        string str2 = str.substr(0, size1);
        ans1 = check1(str1, str2);
      } else
        ans1 = true;
      if (size2 > 0) {
        string str3 = pat.substr(star + 1, size2);
        string str4 = str.substr(m - (size2), size2);
        ans2 = check1(str3, str4);
      } else
        ans2 = true;
      if (ans1 && ans2) {
        int i;
        for (i = star; i < m - size2; i++) {
          if (status[str[i] - 'a']) break;
        }
        if (i == m - size2)
          cout << "YES" << endl;
        else
          cout << "NO" << endl;
      } else
        cout << "NO" << endl;
    }
  }
  return 0;
}
