#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int good[30];
char s[N], t[N];
int main() {
  scanf("%s", s);
  int len = strlen(s);
  for (int i = 0; i < len; i++) good[s[i] - 'a'] = 1;
  scanf("%s", s);
  len = strlen(s);
  int loc = -1;
  for (int i = 0; i < len; i++) {
    if (s[i] == '*') loc = i;
  }
  int q, flag = 0;
  scanf("%d", &q);
  while (q--) {
    flag = 0;
    int ans = 1;
    scanf("%s", t);
    int len2 = strlen(t);
    int start = 0, end1 = len2;
    if (loc < 0) {
      if (len2 != len)
        ans = 0;
      else {
        for (int i = 0; i < len; i++) {
          if (s[i] == t[i] || (s[i] == '?' && good[t[i] - 'a']))
            continue;
          else {
            ans = 0;
            flag = 1;
            break;
          }
        }
      }
    } else {
      if (len2 < len - 1)
        ans = 0;
      else {
        for (int i = 0; i < loc; i++) {
          start = i + 1;
          if (s[i] == t[i] || (s[i] == '?' && good[t[i] - 'a']))
            continue;
          else {
            ans = 0;
            flag = 1;
            break;
          }
        }
        if (ans) {
          for (int i = len - 1, j = 0; i > loc; i--, j++) {
            end1 = len2 - 1 - j;
            if (s[i] == t[len2 - 1 - j] ||
                (s[i] == '?' && good[t[len2 - 1 - j] - 'a']))
              continue;
            else {
              ans = 0;
              flag = 1;
              break;
            }
          }
        }
        if (ans) {
          for (int i = start; i < end1; i++) {
            if (good[t[i] - 'a'] == 1) {
              ans = 0;
            }
          }
        }
      }
    }
    if (ans)
      printf("YES\n");
    else
      printf("NO\n");
  }
}
