#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T sqr(T t) {
  return t * t;
}
inline void files() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
string good, s;
bool ig[50];
int main() {
  files();
  cin >> good;
  for (int i = 0; i < good.size(); i++) {
    ig[good[i] - 'a' + 1] = 1;
  }
  cin >> s;
  int n;
  cin >> n;
  while (n--) {
    string t;
    cin >> t;
    bool bb = 1;
    int pos = -1;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '*') {
        pos = i;
        break;
        if (i == t.size()) {
          bb = 0;
          break;
        }
      } else {
        if (s[i] == '?') {
          if (!ig[t[i] - 'a' + 1]) {
            bb = 0;
            break;
          }
        } else if (s[i] != t[i]) {
          bb = 0;
          break;
        }
      }
    }
    if (bb == 0) {
      cout << "NO"
           << "\n";
      continue;
    }
    if (pos == -1) {
      if (s.size() != t.size())
        cout << "NO"
             << "\n";
      else {
        if (bb)
          cout << "YES"
               << "\n";
        else
          cout << "NO"
               << "\n";
      }
      continue;
    }
    int sz = t.size() - s.size() + 1;
    if (sz < 0) {
      cout << "NO"
           << "\n";
      continue;
    }
    bool bbb = 0;
    bool curb = 1;
    for (int i = pos; i < pos + sz; i++) {
      if (ig[t[i] - 'a' + 1]) {
        curb = 0;
        break;
      }
    }
    int kek = 1;
    for (int i = pos + sz; i < t.size(); i++) {
      if (s[pos + kek] == '?') {
        if (!ig[t[i] - 'a' + 1]) {
          curb = 0;
          break;
        }
      } else if (t[i] != s[pos + kek]) {
        curb = 0;
        break;
      }
      kek++;
    }
    if (curb) {
      bbb = 1;
    }
    if (!bbb)
      cout << "NO"
           << "\n";
    else
      cout << "YES"
           << "\n";
  }
}
