#include <bits/stdc++.h>
using namespace std;
const int MX = 1e5 + 10;
int vis[30];
char good[30];
char s[MX], str[MX];
int main() {
  scanf("%s", good);
  int len1 = strlen(good);
  for (int i = 0; i < len1; i++) {
    int x = good[i] - 'a';
    vis[x] = 1;
  }
  scanf("%s", s);
  int len2 = strlen(s);
  int p = 0;
  for (int i = 0; i < len2; i++) {
    if (s[i] == '*') {
      p = 1;
      break;
    }
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int flag = 1;
    scanf("%s", str);
    int len3 = strlen(str);
    if (len3 > len2 && p == 0) flag = 0;
    int j = 0;
    for (int i = 0; i < len3, j < len2; i++) {
      if (flag == 0) break;
      if (str[i] != s[j]) {
        if (s[j] == '?') {
          int x = str[i] - 'a';
          if (!vis[x]) {
            flag = 0;
            break;
          }
        } else if (s[j] == '*') {
          if (len2 == len3) {
            int x = str[i] - 'a';
            if (vis[x]) {
              flag = 0;
              break;
            }
          } else if (len2 > len3) {
            if (len2 - len3 > 1) flag = 0;
            i--;
          } else {
            int l = len3 - len2;
            for (int k = 0; k <= l; k++) {
              int x = str[i + k] - 'a';
              if (vis[x]) {
                flag = 0;
                break;
              }
            }
            i += l;
          }
        } else {
          flag = 0;
          break;
        }
      }
      j++;
    }
    if (j != len2) flag = 0;
    if (flag)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
