#include <bits/stdc++.h>
using namespace std;
map<char, int> mp1;
int main() {
  ios_base::sync_with_stdio(0);
  string s, p, q;
  long long int a, b, c, d, i, j, k, u, v, w = 0, x, y, z;
  cin >> p;
  for (i = 0; i < p.length(); i++) mp1[p[i]] = 1;
  cin >> s;
  cin >> k;
  while (k--) {
    w = 0;
    cin >> q;
    if (s.length() > q.length() + 1) {
      cout << "NO" << endl;
      continue;
    }
    for (i = 0; i < q.length() && i < s.length(); i++) {
      if (q[i] == s[i]) continue;
      if (s[i] == '?' && mp1[q[i]] == 1) continue;
      if (s[i] == '*') {
        w = 1;
        v = s.length() - 1;
        for (u = q.length() - 1; v > i && u >= i; u--, v--) {
          if (q[u] == s[v]) continue;
          if (s[v] == '?' && mp1[q[u]] == 1) continue;
          break;
        }
      } else {
        cout << "NO" << endl;
        w = -1;
        break;
      }
      if (v != i) {
        w = -1;
        cout << "NO" << endl;
        break;
      }
      for (j = u; j >= i; j--) {
        if (mp1[q[j]] == 0) continue;
        break;
      }
      if (j >= i) {
        w = -1;
        cout << "NO" << endl;
        break;
      }
      i = q.length() - 1;
    }
    if (w == -1) continue;
    if (i != q.length()) {
      cout << "NO" << endl;
      continue;
    }
    if (s.length() == q.length() + 1 && w == 0 && s[q.length()] != '*') {
      cout << "NO" << endl;
      continue;
    }
    cout << "YES" << endl;
  }
  return 0;
}
