#include <bits/stdc++.h>
using namespace std;
bool vis[128];
int main() {
  string good, s, qry;
  cin >> good >> s;
  for (int i = 0; i < good.size(); i++) vis[good[i]] = 1;
  bool isStar = false, starPos = -1;
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '*') isStar = true, starPos = i;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    cin >> qry;
    if (qry.size() == s.size()) {
      bool ans = true;
      for (int i = 0; i < qry.size(); i++) {
        if (s[i] == '?') {
          if (vis[qry[i]] == 0) ans = false;
        } else if (s[i] == '*') {
          if (vis[qry[i]] == 1) ans = false;
        } else {
          if (qry[i] != s[i]) ans = false;
        }
      }
      printf("%s\n", ans ? "YES" : "NO");
    } else if (isStar == true && qry.size() >= s.size() - 1) {
      bool ans = true;
      for (int i = 0; i < qry.size(); i++) {
        if (s[i] == '*') break;
        if (s[i] == '?') {
          if (vis[qry[i]] == 0) ans = false;
        } else {
          if (qry[i] != s[i]) ans = false;
        }
        qry[i] = '.';
      }
      for (int i = qry.size() - 1, j = s.size() - 1;; i--, j--) {
        if (s[j] == '*') break;
        if (s[j] == '?') {
          if (vis[qry[i]] == 0) ans = false;
        } else {
          if (qry[i] != s[j]) ans = false;
        }
        qry[i] = '.';
      }
      for (int i = 0; i < qry.size(); i++) {
        if (qry[i] != '.' && vis[qry[i]]) ans = false;
      }
      printf("%s\n", ans ? "YES" : "NO");
    } else {
      printf("NO\n");
    }
  }
}
