#include <bits/stdc++.h>
using namespace std;
char s1[100002], s2[100002], s3[100002];
int dis[100002];
int n;
int main() {
  scanf("%s", s1);
  int len1 = strlen(s1);
  for (int i = 0; i < len1; i++) dis[s1[i]] = 1;
  scanf("%s", s2);
  int len2 = strlen(s2);
  int ff = 0;
  for (int i = 0; i < len2; i++)
    if (s2[i] == '*') ff = 1;
  cin >> n;
  while (n--) {
    int flag = 0;
    scanf("%s", s3);
    int len3 = strlen(s3);
    int num2 = 0, num3 = 0;
    if (len3 < len2 - 1 && ff)
      printf("NO\n");
    else if (len3 < len2 && !ff)
      printf("NO\n");
    else if (len3 > len2 && !ff)
      printf("NO\n");
    else {
      int len = len3 - len2 + 1;
      for (int i = 0; i < len2; i++) {
        if (s2[i] == '?') {
          if (!dis[s3[num3]]) {
            flag = 1;
            break;
          }
          num3++;
        } else if (s2[i] == '*') {
          for (int j = i; j < i + len; j++) {
            if (dis[s3[j]]) {
              flag = 1;
              break;
            }
          }
          num3 += len;
        } else {
          if (s2[i] != s3[num3]) {
            flag = 1;
            break;
          }
          num3++;
        }
        if (flag) break;
      }
      if (flag)
        printf("NO\n");
      else
        printf("YES\n");
    }
  }
  return 0;
}
