#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cout.setf(ios::fixed);
  cout.precision(20);
  string g, in, query;
  int n, count;
  set<char> good;
  cin >> g;
  for (auto v : g) good.insert(v);
  cin >> in >> n;
  int hasStar = 0;
  for (auto v : in)
    if (v == '*') hasStar = 1;
  for (int k = (1); k <= (n); ++k) {
    cin >> query;
    if ((!hasStar && query.size() != in.size()) ||
        (query.size() < in.size() - 1))
      cout << "NO\n";
    else {
      if (query.size() == in.size() - 1)
        count = 0;
      else
        count = query.size() - in.size() + 1;
      int cnt = 0, bad = 0;
      for (int i = (0); i <= (in.size() - 1); ++i) {
        if (in[i] == '?') {
          if (good.count(query[cnt]))
            cnt++;
          else {
            bad = 1;
            break;
          }
        } else if (in[i] == '*') {
          for (int j = (cnt); j <= (cnt + count - 1); ++j)
            if (good.count(query[j])) bad = 1;
          cnt += count;
        } else {
          if (in[i] == query[cnt])
            cnt++;
          else
            bad = 1;
        }
      }
      if (bad)
        cout << "NO\n";
      else
        cout << "YES\n";
    }
  }
  return 0;
}
