#include <bits/stdc++.h>
using namespace std;
int main() {
  string s, t;
  cin >> t >> s;
  set<char> b(t.begin(), t.end());
  int n;
  cin >> n;
  for (int k = 0; k < n; ++k) {
    cin >> t;
    int i = 0;
    int j = 0;
    while (i < s.size() || j < t.size()) {
      if (s[i] == '?') {
        if (b.find(t[j]) != b.end()) {
          ++i;
          ++j;
        } else {
          break;
        }
      } else if (s[i] == '*') {
        int x = t.size() - s.size() + 1;
        if (x < 0) {
          break;
        } else if (all_of(t.begin() + i, t.begin() + i + x,
                          [&](char c) { return b.find(c) == b.end(); })) {
          ++i;
          j += x;
        } else {
          break;
        }
      } else {
        if (s[i] == t[j]) {
          ++i;
          ++j;
        } else {
          break;
        }
      }
    }
    if (i == s.size() && j == t.size())
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
