#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
map<char, long long> m;
bool equals(string s, string p) {
  for (long long i = 0; i < s.length(); i++) {
    if (s[i] != p[i] && p[i] == '?' && m[s[i]] == 0)
      return 0;
    else if (s[i] != p[i] && p[i] != '?')
      return 0;
  }
  return 1;
}
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string S, p;
  cin >> S >> p;
  auto k = p.find('*');
  for (auto i : S) {
    m[i] = 1;
  }
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    if (s.length() < p.length() - 1) {
      cout << "NO\n";
      continue;
    }
    if (k == string::npos) {
      if (s.length() != p.length()) {
        cout << "NO\n";
        continue;
      } else {
        long long c = equals(s, p);
        if (c) {
          cout << "YES\n";
        } else {
          cout << "NO\n";
        }
      }
      continue;
    }
    if (!equals(s.substr(0, k), p.substr(0, k))) {
      cout << "NO\n";
      continue;
    }
    long long x = p.length() - k - 1;
    if (!equals(s.substr(s.length() - x), p.substr(p.length() - x))) {
      cout << "NO\n";
      continue;
    }
    bool b = true;
    for (long long i = k; i < s.length() - x; i++) {
      if (m[s[i]]) {
        b = false;
        break;
      }
    }
    if (b) {
      cout << "YES\n";
    } else {
      cout << "NO\n";
    }
  }
}
