#include <bits/stdc++.h>
using namespace std;
string gd, str, ask;
int q, n, t, ans, tmp, cnt, ara[1000];
int main() {
  cin >> gd >> str >> n;
  for (int i = 0; i < gd.size(); i++) ara[gd[i] - 'a'] = 1;
  while (n--) {
    cin >> ask;
    int in = -1, fin = -1;
    ans = 0;
    for (int i = 0; i < max(str.size(), ask.size()); i++) {
      if (str[i] == '?') {
        if (ara[ask[i] - 'a'] != 1) {
          ans = 2;
          break;
        }
      } else if (str[i] == '*') {
        in = i;
        break;
      } else {
        if (str[i] != ask[i]) {
          ans = 2;
          break;
        }
      }
    }
    if (ans == 2) {
      printf("NO\n");
      continue;
    } else if (in == -1) {
      printf("YES\n");
      continue;
    } else {
      for (int i = str.size() - 1, j = ask.size() - 1; i >= 0; i--, j--) {
        if (str[i] == '?') {
          if (ara[ask[j] - 'a'] != 1) {
            ans = 2;
            break;
          }
        } else if (str[i] == '*') {
          fin = j + 1;
          break;
        } else {
          if (str[i] != ask[j]) {
            ans = 2;
            break;
          }
        }
      }
    }
    if (ans == 2 || fin == -1 || in > fin) {
      printf("NO\n");
      continue;
    } else {
      for (int i = in; i < fin; i++) {
        if (ara[ask[i] - 'a'] == 1) {
          ans = 2;
          break;
        }
      }
    }
    if (ans == 2) {
      printf("NO\n");
    } else
      printf("YES\n");
  }
  return 0;
}
