#include <bits/stdc++.h>
using namespace std;
long long fast_power(long long val, long long deg, long long mod = 1000000007) {
  if (!deg) return 1 % mod;
  if (deg & 1) return fast_power(val, deg - 1, mod) * val % mod;
  long long res = fast_power(val, deg >> 1, mod);
  return (res * res) % mod;
}
long long MMI(long long a, long long mm = 1000000007) {
  return fast_power(a % mm, mm - 2, mm) % mm;
}
map<char, long long> m;
int main() {
  string str;
  cin >> str;
  for (auto &i : str) m[i]++;
  string pat;
  cin >> pat;
  long long q;
  cin >> q;
  while (q--) {
    string sq;
    cin >> sq;
    bool flag = false;
    long long i, j;
    long long khana = (long long)sq.size() - (long long)pat.size() + 1;
    for (i = 0, j = 0; i < (long long)pat.size() and j < (long long)sq.size();
         i++, j++) {
      if (pat[i] == '?') {
        if (m.count(sq[j]) <= 0) {
          cout << "NO"
               << "\n";
          flag = true;
          break;
        }
      } else if (pat[i] == '*') {
        if (khana and m.count(sq[j]) == 0) {
          i--;
          khana--;
          continue;
        }
        j--;
      } else if (pat[i] != sq[j]) {
        cout << "NO"
             << "\n";
        flag = true;
        break;
      }
    }
    if (flag == true) continue;
    if ((long long)pat.size() - 1 == i and pat[i] == '*' and
        (long long) sq.size() == j)
      cout << "YES"
           << "\n";
    else if ((long long)pat.size() == i and (long long) sq.size() == j) {
      cout << "YES"
           << "\n";
    } else
      cout << "NO"
           << "\n";
  }
  return 0;
}
