#include <bits/stdc++.h>
using namespace std;
char good[105];
char a[1000000];
char b[1000000];
int vis[5000];
int main() {
  while (~scanf("%s", good)) {
    memset(vis, 0, sizeof(vis));
    int len = strlen(good);
    for (int i = 0; i < len; i++) {
      vis[good[i]]++;
    }
    scanf("%s", a);
    int n = strlen(a);
    int ok = 1;
    for (int i = 0; i < n; i++) {
      if (a[i] == '?' || a[i] == '*') continue;
      if (vis[a[i]] == 0) {
        ok = 0;
      }
    }
    int q;
    scanf("%d", &q);
    while (q--) {
      scanf("%s", b);
      int m = strlen(b);
      int i = 0;
      int flag = 0;
      for (int j = 0; j < m; j++) {
        if (a[i] == '?') {
          if (vis[b[j]] > 0) {
            i++;
            continue;
          } else {
            flag = 1;
            break;
          }
        } else if (a[i] == '*') {
          int cnt = 0;
          int tmp = m - n + 1;
          while (j < m && cnt < tmp) {
            cnt++;
            if (vis[b[j]] == 0)
              j++;
            else {
              if (a[i + 1] != '?') flag = 1;
              break;
            }
          }
          if (flag == 1) break;
          i++;
          j--;
        } else {
          if (a[i] == b[j]) {
            i++;
            continue;
          } else {
            flag = 1;
            break;
          }
        }
      }
      if (flag == 0 && i == n || (i == n - 1 && flag == 0 && a[i] == '*'))
        printf("YES\n");
      else
        printf("NO\n");
    }
  }
}
