#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 1e9;
const long long LINF = 1e18;
const double FINF = 1e15;
const double EPS = 1e-9;
const double PI = 2.0 * acos(0.0);
bool chk[30];
char a[100005], b[100005];
int n, m;
bool ok(int i, int j) {
  if (a[i] == b[j]) return true;
  if (a[i] == '?' && chk[b[j] - 'a']) return true;
  return false;
}
int main() {
  scanf("%s", a);
  for (int i = 0; a[i]; ++i) {
    chk[a[i] - 'a'] = true;
  }
  scanf("%s", a);
  int t;
  n = strlen(a);
  scanf("%d", &t);
  int star = -1;
  for (int i = 0; i < n; ++i)
    if (a[i] == '*') star = i;
  int cnt1 = 0;
  for (int i = 0; i < n; ++i) {
    if (a[i] != '*') ++cnt1;
  }
  while (t--) {
    scanf("%s", b);
    m = strlen(b);
    bool ans = true;
    if (cnt1 > m) {
      puts("NO");
      continue;
    }
    if (star == -1) {
      if (n != m) ans = false;
      for (int i = 0; i < m; ++i) {
        if (!ok(i, i)) {
          ans = false;
          break;
        }
      }
    } else {
      for (int i = 0; i < star; ++i) {
        if (!ok(i, i)) {
          ans = false;
          break;
        }
      }
      int l = star;
      int r = m - 1;
      for (int i = 0; a[n - i - 1] != '*'; ++i) {
        --r;
        if (!ok(n - i - 1, m - i - 1)) {
          ans = false;
        }
      }
      for (int i = l; i <= r; ++i) {
        if (chk[b[i] - 'a']) {
          ans = false;
          break;
        }
      }
    }
    puts(ans ? "YES" : "NO");
  }
}
