#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  char gls[27], s[100001], q[100001];
  int n;
  bool gl[123] = {false};
  scanf("%s %s %d", gls, s, &n);
  for (int i = 0; gls[i]; ++i) {
    gl[gls[i]] = true;
  }
  int lens = strlen(s);
  int idx = -1;
  for (int i = 0; s[i]; ++i) {
    if (s[i] == '*') {
      idx = i;
      break;
    }
  }
  for (int i = 0; i < n; ++i) {
    scanf("%s", q);
    int lenq = strlen(q);
    if (idx == -1 && lenq != lens) {
      printf("NO\n");
      continue;
    }
    if (idx != -1 && lenq + 1 < lens) {
      printf("NO\n");
      continue;
    }
    bool ok = true;
    for (int j = 0; j < idx; ++j) {
      if (s[j] == '?') {
        if (!gl[q[j]]) {
          ok = false;
          break;
        }
      } else if (s[j] != q[j]) {
        ok = false;
        break;
      }
    }
    if (!ok) {
      printf("NO\n");
      continue;
    }
    for (int j = 0; j < lens - idx - 1; ++j) {
      if (s[lens - 1 - j] == '?') {
        if (!gl[q[lenq - 1 - j]]) {
          ok = false;
          break;
        }
      } else if (s[lens - 1 - j] != q[lenq - 1 - j]) {
        ok = false;
        break;
      }
    }
    if (!ok) {
      printf("NO\n");
      continue;
    }
    for (int j = idx; j <= idx + lenq - lens; ++j) {
      if (gl[q[j]]) {
        ok = false;
        break;
      }
    }
    if (!ok)
      printf("NO\n");
    else
      printf("YES\n");
  }
  return 0;
}
