#include <bits/stdc++.h>
using namespace std;
int viz[30], ok;
char bun[30], *p, s[100001], sir[100001];
long int n, lungs = 0, lungsir = 0, ceva;
int main() {
  long int i, j, k, ct;
  cin >> bun;
  p = bun;
  while (*p != 0) {
    viz[*p - 'a'] = 1;
    p++;
  }
  cin >> s >> n;
  lungs = strlen(s);
  for (i = 1; i <= n; i++) {
    cin >> sir;
    lungsir = strlen(sir);
    ceva = lungsir - lungs + 1;
    ok = 1;
    k = 0;
    for (j = 0; (j < lungs) && ok; j++)
      if ((s[j] >= 'a') && (s[j] <= 'z')) {
        if (s[j] != sir[k])
          ok = 0;
        else
          k++;
      } else if (s[j] == '?') {
        if (!viz[sir[k] - 'a'])
          ok = 0;
        else
          k++;
      } else if (s[j] == '*') {
        for (ct = 0; (ct < ceva) && ok; ct++)
          if (!viz[sir[k] - 'a'])
            k++;
          else
            ok = 0;
      }
    if ((ok) && (k == lungsir))
      cout << "YES"
           << "\n";
    else
      cout << "NO"
           << "\n";
  }
  return 0;
}
