#include <bits/stdc++.h>
using namespace std;
int main() {
  string a, b, c, star_s;
  int q, pos = -1, last;
  bool star = false;
  map<char, bool> valid;
  cin >> a >> b >> q;
  for (int i = 0; i < a.size(); i++) {
    valid[a[i]] = true;
  }
  for (int i = 0; i < b.size(); i++) {
    if (b[i] == '*') {
      star = true;
      pos = i;
      last = b.size() - pos - 1;
      break;
    }
  }
  while (q--) {
    cin >> c;
    star_s.clear();
    if (star && c.size() + 1 >= b.size()) {
      bool t = true;
      for (int i = pos; pos != -1 && i < c.size() - last; i++) {
        star_s += c[i];
      }
      for (int i = 0; i < star_s.size(); i++) {
        if (valid[star_s[i]]) t = false;
      }
      for (int i = 0, j = 0; i < b.size(); i++, j++) {
        if (b[i] == '*') {
          j += star_s.size() - 1;
        } else if (b[i] == '?') {
          if (!valid[c[j]]) {
            t = false;
            break;
          }
        } else {
          if (b[i] != c[j]) {
            t = false;
            break;
          }
        }
      }
      if (t) {
        cout << "YES" << endl;
      } else {
        cout << "NO" << endl;
      }
    } else if (c.size() == b.size()) {
      bool t = true;
      for (int i = 0; i < b.size(); i++) {
        if (b[i] == '?') {
          if (!valid[c[i]]) {
            t = false;
            break;
          }
        } else if (b[i] != c[i]) {
          t = false;
          break;
        }
      }
      if (t) {
        cout << "YES" << endl;
      } else {
        cout << "NO" << endl;
      }
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}
