#include <bits/stdc++.h>
using namespace std;
int g[26];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  string good;
  cin >> good;
  for (int i = 0; i < good.size(); i++) g[good[i] - 'a'] = 1;
  string pat;
  cin >> pat;
  pat += "^";
  int n;
  cin >> n;
  for (int nn = 0; nn < n; nn++) {
    string s;
    cin >> s;
    s += "^";
    if (s.size() < pat.size() - 1) {
      cout << "NO" << endl;
      continue;
    }
    bool ok = 1;
    int i = 0, j = 0;
    int k = s.size() - 1, l = pat.size() - 1;
    for (; i < s.size() and j < pat.size() and pat[j] != '*';) {
      if (s[i] == pat[j])
        ok &= 1, i++, j++;
      else if (g[s[i] - 'a'] == 1 and pat[j] == '?')
        ok &= 1, i++, j++;
      else {
        ok &= 0;
        break;
      }
    }
    for (; k >= 0 and l >= 0 and pat[l] != '*';) {
      if (s[k] == pat[l])
        ok &= 1, k--, l--;
      else if (g[s[k] - 'a'] == 1 and pat[l] == '?')
        ok &= 1, k--, l--;
      else {
        ok &= 0;
        break;
      }
    }
    if (k == -1 and l == -1 and i == s.size() and j == pat.size()) {
      cout << "YES" << endl;
      continue;
    }
    if (j != l) ok &= 0;
    for (; i <= k; i++) {
      if (g[s[i] - 'a'] == 1) ok &= 0;
    }
    if (!ok)
      cout << "NO" << endl;
    else
      cout << "YES" << endl;
  }
  return 0;
}
