#include <bits/stdc++.h>
const int N = 1e5 + 5;
int n, vis[26];
char s[N], s0[N];
int main() {
  scanf("%s%s%d", s, s0, &n);
  int fl = 1;
  for (register int i = 0; i < strlen(s); ++i) vis[s[i] - 'a'] = 1;
  for (register int j = 0; j < strlen(s0); ++j)
    if (s0[j] == '*') {
      fl = 0;
      break;
    }
  for (register int i = 0; i < n; ++i) {
    scanf("%s", s);
    int ct = 0, f = 1, ds = strlen(s) - strlen(s0) + 1;
    if (fl) {
      if (strlen(s) != strlen(s0)) {
        puts("NO");
        continue;
      }
      for (register int j = 0; j < strlen(s); ++j)
        if ((s0[j] == '?' && !vis[s[j] - 'a']) ||
            (s0[j] != '?' && s0[j] != s[j])) {
          f = 0;
          break;
        }
      if (f)
        puts("YES");
      else
        puts("NO");
      continue;
    }
    if (strlen(s) < strlen(s0) - 1) {
      puts("NO");
      continue;
    }
    for (register int j = 0; j < strlen(s); ++j)
      if (s0[ct] != '?' && s0[ct] != '*') {
        if (s[j] != s0[ct]) {
          f = 0;
          break;
        } else
          ++ct;
      } else if (s0[ct] == '?') {
        if (!vis[s[j] - 'a']) {
          f = 0;
          break;
        } else
          ++ct;
      } else if (s0[ct] == '*') {
        for (register int k = 0; k < ds; ++k, ++j)
          if (vis[s[j] - 'a']) {
            f = 0;
            break;
          }
        if (!f)
          break;
        else
          ++ct, --j;
      }
    if (f)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
