#include <bits/stdc++.h>
using namespace std;
int n;
string P;
bool G[26];
int bp;
int Q;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string gstr;
  cin >> gstr;
  for (char ci : gstr) G[(int)(ci - 'a')] = 1;
  cin >> P;
  n = (int)(P.size());
  bp = -1;
  for (int i = 0; i <= n - 1; i++)
    if (P[i] == '*') bp = i;
  cin >> Q;
  string str;
  for (int q = 1; q <= Q; q++) {
    cin >> str;
    int m = (int)(str.size());
    if (bp == -1) {
      if (m != n) {
        cout << "NO\n";
        continue;
      }
      bool F = 0;
      for (int i = 0; i <= m - 1; i++) {
        if (P[i] == '?') {
          if (!G[(int)(str[i] - 'a')]) {
            F = 1;
            break;
          }
        } else if (str[i] != P[i]) {
          F = 1;
          break;
        }
      }
      cout << (F ? "NO\n" : "YES\n");
    } else {
      if (m < n - 1) {
        cout << "NO\n";
        continue;
      }
      bool F = 0;
      for (int i = 0; i <= bp - 1; i++) {
        if (P[i] == '?') {
          if (!G[(int)(str[i] - 'a')]) {
            F = 1;
            break;
          }
        } else if (str[i] != P[i]) {
          F = 1;
          break;
        }
      }
      for (int i = n - 1; i >= bp + 1; i--) {
        if (P[i] == '?') {
          if (!G[(int)(str[m - (n - i)] - 'a')]) {
            F = 1;
            break;
          }
        } else if (str[m - (n - i)] != P[i]) {
          F = 1;
          break;
        }
      }
      for (int i = bp; i <= m - 1 - (n - 1 - bp); i++) {
        if (G[(int)(str[i] - 'a')]) {
          F = 1;
          break;
        }
      }
      cout << (F ? "NO\n" : "YES\n");
    }
  }
  return 0;
}
