#include <bits/stdc++.h>
using namespace std;
struct node {};
const int N = 10;
string str1, str2, str3;
int n, len;
bool book[128], flag = 0;
bool input() {
  memset(book, 0, sizeof(book));
  string ch;
  cin >> ch;
  len = ch.length();
  for (int i = 0; i < len; i++) book[ch[i]] = 1;
  cin >> str1;
  int temp;
  len = str1.length();
  if ((temp = str1.find("*")) != string::npos) {
    flag = 1;
    str2 = str1.substr(0, temp);
    str3 = str1.substr(temp + 1, len - temp - 1);
  }
  scanf("%d", &n);
  return true;
}
int main() {
  input();
  while (n--) {
    string t;
    cin >> t;
    int l = t.length();
    bool f = 1;
    if (!flag && len == l) {
      for (int i = 0; i < l; i++) {
        if (str1[i] == '?') {
          if (!book[t[i]]) {
            f = 0;
            break;
          }
        } else if (str1[i] != t[i]) {
          f = 0;
          break;
        }
      }
    } else if (flag && len - 1 <= l) {
      int len2 = str2.length();
      for (int i = 0; i < len2; i++) {
        if (str2[i] == '?') {
          if (!book[t[i]]) {
            f = 0;
            break;
          }
        } else if (str2[i] != t[i]) {
          f = 0;
          break;
        }
      }
      int len3 = str3.length();
      for (int i = len2; i < l - len3; i++) {
        if (book[t[i]]) {
          f = 0;
          break;
        }
      }
      for (int i = len3 - 1, j = l - 1; i >= 0; i--, j--) {
        if (str3[i] == '?') {
          if (!book[t[j]]) {
            f = 0;
            break;
          }
        } else if (str3[i] != t[j]) {
          f = 0;
          break;
        }
      }
    } else
      f = 0;
    if (f)
      printf("YES\n");
    else
      printf("NO\n");
  }
}
