#include <bits/stdc++.h>
using namespace std;
long long arr[100005];
const long long N = 1e9;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, i, j, k, l, ignore, query, m;
  string s, str, txt, cpy;
  map<char, long long> good, bad;
  cin >> s;
  cin >> str;
  cpy = str;
  for (i = 0; i < s.size(); i++) {
    good[s[i]] = 1;
  }
  for (char c = 'a'; c <= 'z'; c++) {
    if (!good[c]) bad[c] = 1;
  }
  cin >> query;
  n = str.size();
  while (query--) {
    ignore = 1;
    str = cpy;
    cin >> txt;
    m = txt.size();
    if ((n - m) >= 2) {
      cout << "NO" << endl;
      continue;
    }
    if ((n - m) == 1) {
      ignore = 0;
    }
    i = 0;
    j = 0;
    string tmp;
    while (i < n && j < m) {
      if (str[i] == txt[j]) {
        i++;
        j++;
      } else {
        if (str[i] == '?') {
          if (good[txt[j]]) {
            str[i] = txt[j];
          }
          i++;
          j++;
        } else if (str[i] == '*') {
          if (ignore) {
            for (k = j; k <= j + (m - n); k++) {
              if (bad[txt[k]]) tmp += txt[k];
            }
            j = k;
            i++;
          } else {
            i++;
          }
        } else {
          i++;
          j++;
        }
      }
    }
    string ans;
    for (i = 0; i < str.size(); i++) {
      if (str[i] == '*' && ignore) {
        ans += tmp;
      } else if (str[i] != '*') {
        ans += str[i];
      }
    }
    if (ans == txt) {
      cout << "YES" << endl;
    } else
      cout << "NO" << endl;
  }
}
