#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, idx;
map<char, long long> mp;
string s, s1;
int main() {
  cin >> s;
  for (long long i = 0; i < s.size(); i++) {
    mp[s[i]] = 1;
  }
  cin >> s >> n;
  for (long long i = 0; i < s.size(); i++)
    if (s[i] == '*') e = 1;
  for (long long i = 1; i <= n; i++) {
    cin >> s1;
    idx = 0;
    l = 0;
    g = -1;
    if (e == 0) {
      if (s1.size() != s.size()) {
        cout << "NO" << endl;
        continue;
      }
    }
    if ((s1.size() < s.size() - 1)) {
      cout << "NO" << endl;
      continue;
    }
    for (long j = 0; j < s1.size(); j++) {
      if (s[j] == s1[j]) {
        continue;
      } else if (s[j] == '?') {
        if (mp[s1[j]] != 1) {
          cout << "NO" << endl;
          l = 1;
          break;
        }
      } else if (s[j] == '*') {
        g = j;
        break;
      } else {
        cout << "NO" << endl;
        l = 1;
        break;
      }
    }
    if (l == 1) continue;
    if (g == -1) {
      cout << "YES" << endl;
      continue;
    }
    idx = s1.size() - 1;
    for (long long j = s.size() - 1; j > g; j--) {
      if (s[j] == s1[idx]) {
        idx--;
      } else if (s[j] == '?') {
        if (mp[s1[idx]] != 1) {
          cout << "NO" << endl;
          l = 1;
          break;
        } else
          idx--;
      } else if (s[j] == '*')
        break;
      else {
        cout << "NO" << endl;
        l = 1;
        break;
      }
    }
    if (l == 1) continue;
    for (long long j = g; j <= idx; j++)
      if (mp[s1[j]] == 1) {
        cout << "NO" << endl;
        l = 1;
        break;
      }
    if (l == 1) continue;
    cout << "YES" << endl;
  }
}
