#include <bits/stdc++.h>
using namespace std;
const int MX = 111111;
int main() {
  char s1[MX], s2[MX];
  int a[44];
  while (cin >> s2) {
    scanf("%s", s1);
    int len1 = strlen(s1);
    memset(a, 0, sizeof(a));
    ;
    for (int i = 0; i < strlen(s2); i++) a[s2[i] - 'a'] = 1;
    int n;
    cin >> n;
    int t = 0;
    for (char *ss = s1; *ss; ss++)
      if (*ss == '*') t = 1;
    for (int i = 1; i <= n; i++) {
      int flag = 1;
      scanf("%s", s2);
      int len2 = strlen(s2);
      int k1 = 0, k2 = 0;
      if ((t == 0 && len1 == len2) || (t == 1 && (len2 >= len1 - 1))) {
        for (; k1 < len1 && k2 < len2;) {
          if (s1[k1] == '?') {
            if (a[s2[k2] - 'a'] == 0) {
              flag = 0;
              break;
            }
            k1++, k2++;
          } else if (s1[k1] == '*') {
            if (len1 == len2 + 1)
              k1++;
            else if (len1 == len2) {
              if (a[s2[k2] - 'a'] == 1) {
                flag = 0;
                break;
              }
              k1++, k2++;
            } else {
              int w = k2 + len2 - len1;
              while (k2 <= w) {
                if (a[s2[k2] - 'a'] == 1) {
                  flag = 0;
                  break;
                }
                k2++;
              }
              if (flag == 0) break;
              k1++;
            }
          } else if (s2[k2] != s1[k1]) {
            flag = 0;
            break;
          } else
            k1++, k2++;
        }
      } else
        flag = 0;
      puts(flag ? "YES" : "NO");
    }
  }
  return 0;
}
