#include <bits/stdc++.h>
using namespace std;
vector<char> good;
bool ok_symb(char a, char b) {
  if (a == b) return true;
  if (a == '?') {
    for (int i = 0; i < good.size(); ++i) {
      if (good[i] == b) return true;
    }
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string c;
  cin >> c;
  for (int i = 0; i < c.size(); ++i) {
    good.push_back(c.at(i));
  }
  string s;
  cin >> s;
  bool has = false;
  for (int i = 0; i < s.size(); ++i) {
    if (s[i] == '*') has = true;
  }
  int m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    string cur;
    cin >> cur;
    bool ok = true;
    if (!has) {
      if (cur.size() != s.size()) {
        cout << "NO"
             << "\n";
        continue;
      }
      for (int j = 0; j < cur.size(); ++j) {
        if (!ok_symb(s[j], cur[j])) ok = false;
      }
      if (!ok) {
        cout << "NO"
             << "\n";
      } else {
        cout << "YES"
             << "\n";
      }
    } else {
      if (s.size() - 1 > cur.size()) {
        cout << "NO"
             << "\n";
        continue;
      }
      int l, r;
      for (int j = 0; j < s.size(); ++j) {
        if (s[j] != '*') {
          if (!ok_symb(s[j], cur[j])) {
            ok = false;
            break;
          }
        } else {
          l = j;
          break;
        }
      }
      for (int j = 0; j < s.size(); ++j) {
        int inds = s.size() - j - 1;
        int indcur = cur.size() - j - 1;
        if (s[inds] != '*') {
          if (!ok_symb(s[inds], cur[indcur])) {
            ok = false;
            break;
          }
        } else {
          r = indcur;
          break;
        }
      }
      if (!ok) {
        cout << "NO"
             << "\n";
        continue;
      }
      if (cur.size() + 1 == s.size()) {
        cout << "YES"
             << "\n";
        continue;
      }
      for (int j = l; j <= r; ++j) {
        for (int k = 0; k < good.size(); ++k) {
          if (good[k] == cur[j]) ok = false;
        }
      }
      if (!ok) {
        cout << "NO"
             << "\n";
      } else {
        cout << "YES"
             << "\n";
      }
    }
  }
}
