#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
const int INF = 0x3f3f3f3f;
const int mod = 1e9;
int T[500];
int main() {
  string right;
  while (cin >> right) {
    memset(T, 0, sizeof(T));
    for (int i = 0; i < right.size(); i++) {
      T[right[i] - 'a'] = 1;
    }
    string s;
    cin >> s;
    bool x = false;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '*') {
        x = true;
      }
    }
    int N;
    cin >> N;
    for (int i = 1; i <= N; i++) {
      int flag = 1;
      string t;
      cin >> t;
      if (t.size() >= s.size()) {
        int down = 0;
        int xing = 0;
        for (int i = 0; i < s.size() && flag; i++) {
          if (s[i] == '*') {
            down = -s.size() + t.size() + 1;
            for (int j = i; j < i + down; j++) {
              if (T[t[j] - 'a']) {
                flag = 0;
                break;
              }
            }
            xing = 1;
            down = -down;
            down += 1;
          } else if (s[i] == '?') {
            if (t[i - down] < 'a' || t[i - down] > 'z' ||
                !T[t[i - down] - 'a']) {
              flag = 0;
              break;
            }
          } else if (t[i - down] < 'a' || t[i - down] > 'z' ||
                     s[i] != t[i - down]) {
            flag = 0;
            break;
          }
        }
        if (t.size() > s.size() && xing == 0) {
          printf("NO\n");
        } else if (flag) {
          printf("YES\n");
        } else {
          printf("NO\n");
        }
      } else if (t.size() == s.size() - 1) {
        if (x) {
          int down = 0;
          for (int i = 0; i < s.size(); i++) {
            if (s[i] == '*') {
              down = 1;
            } else if (s[i] == '?') {
              if (t[i - down] < 'a' || t[i - down] > 'z' ||
                  !T[t[i - down] - 'a']) {
                flag = 0;
                break;
              }
            } else if (t[i - down] < 'a' || t[i - down] > 'z' ||
                       s[i] != t[i - down]) {
              flag = 0;
              break;
            }
          }
          if (flag) {
            printf("YES\n");
          } else {
            printf("NO\n");
          }
        } else {
          printf("NO\n");
          continue;
        }
      } else {
        printf("NO\n");
      }
    }
  }
  return 0;
}
