#include <bits/stdc++.h>
using namespace std;
const int MAX = 1e5 + 4;
bool goodChar[26];
int containsAss(string str) {
  for (int i = 0; i < str.length(); ++i) {
    if (str[i] == '*') return i;
  }
  return -1;
}
int main() {
  string gs, str, q;
  cin >> gs;
  for (int i = 0; i < gs.length(); ++i) goodChar[gs[i] - 'a'] = true;
  cin >> str;
  int len = str.length();
  int assAt = containsAss(str);
  if (assAt == -1) {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
      bool flag = true;
      cin >> q;
      if (q.length() == len) {
        for (int i = 0; i < q.length(); ++i) {
          if (str[i] == '?' && goodChar[q[i] - 'a'])
            ;
          else if (str[i] == q[i])
            ;
          else
            flag = false;
        }
      } else
        flag = false;
      if (flag)
        cout << "YES\n";
      else
        cout << "NO\n";
    }
    return 0;
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    bool flag = true;
    cin >> q;
    if (q.length() >= len - 1) {
      for (int i = 0; i < assAt; ++i) {
        if (str[i] == '?' && goodChar[q[i] - 'a'])
          ;
        else if (str[i] == q[i])
          ;
        else
          flag = false;
      }
      int j = q.length() - 1;
      for (int i = len - 1; i > assAt; --i, --j) {
        if (str[i] == '?' && goodChar[q[j] - 'a'])
          ;
        else if (str[i] == q[j])
          ;
        else
          flag = false;
      }
      for (int i = assAt; i < j + 1; ++i) {
        if (goodChar[q[i] - 'a']) flag = false;
      }
    } else
      flag = false;
    if (flag)
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}
