#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long i, star = 0, ind;
  string s1, s;
  cin >> s1;
  map<char, long long> mp;
  for (i = 0; i < int(s1.size()); i++) mp[s1[i]] = 1;
  cin >> s;
  for (i = 0; i < int(s.size()); i++) {
    if (s[i] == '*') {
      star = 1;
      ind = i;
    }
  }
  long long n = s.size();
  long long t;
  cin >> t;
  while (t--) {
    string s1;
    cin >> s1;
    long long m = s1.size();
    if (star == 0) {
      if (n != m) {
        cout << "NO" << endl;
        continue;
      } else {
        long long flag = 0;
        for (i = 0; i < n; i++) {
          if (s[i] != '?' and s[i] != s1[i]) {
            flag = 1;
            break;
          } else if (s[i] == '?' and mp[s1[i]] == 0) {
            flag = 1;
            break;
          }
        }
        if (flag == 0)
          cout << "YES" << endl;
        else
          cout << "NO" << endl;
      }
    } else if (star == 1) {
      if (m < (n - 1)) {
        cout << "NO" << endl;
        continue;
      } else {
        long long flag = 0;
        for (i = 0; i < ind; i++) {
          if (s[i] != '?' and s[i] != s1[i]) {
            flag = 1;
            break;
          } else if (s[i] == '?' and mp[s1[i]] == 0) {
            flag = 1;
            break;
          }
        }
        if (flag == 1) {
          cout << "NO" << endl;
          continue;
        }
        long long p1 = n - 1, p2 = m - 1;
        while (s[p1] != '*') {
          if (s[p1] != '?' and s[p1] != s1[p2]) {
            flag = 1;
            break;
          } else if (s[p1] == '?' and mp[s1[p2]] == 0) {
            flag = 1;
            break;
          }
          p1--;
          p2--;
        }
        if (flag == 1) {
          cout << "NO" << endl;
          continue;
        }
        long long l = n - ind - 1;
        string str = s1.substr(ind, (m - ind - l));
        for (i = 0; i < int(str.size()); i++) {
          if (mp[str[i]] == 1) flag = 1;
        }
        if (flag == 1)
          cout << "NO" << endl;
        else
          cout << "YES" << endl;
      }
    }
  }
  return 0;
}
