#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int d[1000];
void xuly(string a) {
  int l1 = s2.length();
  int l2 = a.length();
  int i = 0, j = 0;
  while (i < l2)
    if (a[i] == s2[j])
      i++, j++;
    else if (s2[j] == '?' && d[(int)a[i]])
      i++, j++;
    else if (s2[j] == '*' && !d[(int)a[i]] && i <= l2 - l1 + j)
      i++;
    else if (s2[j] == '*')
      j++;
    else
      break;
  if (s2[j] == '*') j++;
  if (i == l2 && j == l1)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> s1;
  cin >> s2;
  memset(d, 0, sizeof(d));
  for (int i = 0; i < s1.length(); i++) d[(int)s1[i]] = true;
  int k;
  cin >> k;
  for (int i = 1; i <= k; i++) {
    string m;
    cin >> m;
    if (m.length() < s2.length() - 1) {
      cout << "NO" << endl;
      continue;
    }
    xuly(m);
  }
  return 0;
}
