#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(false);
  string s;
  cin >> s;
  bool letters[27];
  memset(letters, 0, sizeof(letters));
  for (int i = 0; i < s.length(); i++) {
    letters[s[i] - 'a'] = 1;
  }
  int desno = 0;
  string ans;
  int n;
  cin >> ans;
  cin >> n;
  int pre = 0, posle = 0;
  for (int i = 0; i < ans.length(); i++) {
    if (ans[i] == '*') break;
    pre++;
  }
  int pos = 9999999;
  for (int i = ans.length() - 1; i >= 0; i--) {
    if (ans[i] == '*') {
      pos = i;
      break;
    }
    posle++;
  }
  for (int i = 0; i < n; i++) {
    string pitanje;
    cin >> pitanje;
    bool b = 1;
    if (pitanje.length() < ans.length())
      desno = 1;
    else
      desno = 0;
    if (pos == 9999999 &&
        (pitanje.length() > ans.length() || pitanje.length() < ans.length()))
      b = 0;
    if (pitanje.length() >= ans.length() - 1) {
      for (int i = 0; i < pre; i++) {
        if (ans[i] == '?') {
          if (letters[pitanje[i] - 'a'] == 0) {
            b = 0;
            break;
          }
        } else {
          if (ans[i] != pitanje[i]) {
            b = 0;
            break;
          }
        }
      }
      for (int i = ans.length() - 1, j = pitanje.length() - 1;
           i != (ans.length() - posle - 1); i--, j--) {
        if (ans[i] == '?') {
          if (letters[pitanje[j] - 'a'] == 0) {
            b = 0;
            break;
          }
        } else {
          if (ans[i] != pitanje[j]) {
            b = 0;
            break;
          }
        }
      }
      if (pos != 9999999 && pitanje.length() >= ans.length()) {
        for (int i = pos; i <= pos + pitanje.length() - ans.length(); i++) {
          if (letters[pitanje[i] - 'a'] == 1) {
            b = 0;
            break;
          }
        }
      }
    } else
      b = 0;
    if (b)
      cout << "YES"
           << "\n";
    else
      cout << "NO"
           << "\n";
  }
  return 0;
}
