#include <bits/stdc++.h>
using namespace std;
set<char> save;
int main() {
  ios::sync_with_stdio(false);
  string good, s;
  int n, num = 0;
  cin >> good;
  for (int i = 0; i < good.size(); i++) save.insert(good[i]);
  cin >> s >> n;
  for (int i = 0; i < s.size(); i++)
    if (s[i] != '*') num++;
  for (int i = 1; i <= n; i++) {
    string q;
    cin >> q;
    int ok = 1, pos1 = -1, pos2 = -1;
    if (num == s.size() && num != q.size()) ok = 0;
    if (num == s.size() - 1 && num > q.size()) ok = 0;
    for (int j = 0, k = 0; j < q.size() && k < s.size(); j++, k++) {
      if (s[k] == '*') {
        pos1 = k;
        break;
      } else if (s[k] != '?') {
        if (s[k] != q[j]) {
          ok = 0;
          break;
        }
      } else {
        if (save.find(q[j]) == save.end()) {
          ok = 0;
          break;
        }
      }
    }
    for (int j = q.size() - 1, k = s.size() - 1; j >= 0 && k >= 0; j--, k--) {
      if (s[k] == '*') {
        pos2 = j;
        break;
      } else if (s[k] != '?') {
        if (s[k] != q[j]) {
          ok = 0;
          break;
        }
      } else {
        if (save.find(q[j]) == save.end()) {
          ok = 0;
          break;
        }
      }
    }
    if (pos1 != -1 && pos2 != -1) {
      for (int j = pos1; j <= pos2; j++)
        if (save.find(q[j]) != save.end()) {
          ok = 0;
          break;
        }
    }
    if (ok)
      cout << "YES" << endl;
    else
      cout << "NO" << endl;
  }
  return 0;
}
