#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 100;
const int mod = 1e9 + 7;
bool vis[maxn];
int main() {
  string s, ss;
  cin >> s >> ss;
  for (int i = 0; i < s.size(); i++) {
    vis[s[i] - 'a' + 1] = 1;
  }
  int f = 0;
  for (int i = 0; i < ss.size(); i++) {
    if (ss[i] == '*') f = 1;
  }
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string a;
    cin >> a;
    if (f == 0 && a.size() != ss.size()) {
      cout << "NO" << endl;
      continue;
    }
    int flag = 0;
    int p = a.size() - ss.size() + 1;
    if (p < 0) {
      cout << "NO" << endl;
      continue;
    }
    int k = 0, j;
    for (j = 0; j < a.size(); j++, k++) {
      if (a[j] == ss[k]) continue;
      if (ss[k] == '?' && vis[a[j] - 'a' + 1]) continue;
      if (ss[k] == '*') {
        for (int x = 0; x < p; x++) {
          if (vis[a[x + j] - 'a' + 1] != 0) {
            flag = 1;
            break;
          }
        }
        if (flag) break;
        j += p - 1;
        continue;
      }
      flag = 1;
      break;
    }
    if (!flag && (k == ss.size() || (k == ss.size() - 1 && ss[k] == '*'))) {
      cout << "YES" << endl;
    } else
      cout << "NO" << endl;
  }
  return 0;
}
