#include <bits/stdc++.h>
using namespace std;
bool l[32];
int main() {
  int n;
  string g, s;
  cin >> g;
  for (int i = 0; i < g.size(); i++) l[g[i] - 'a'] = true;
  cin >> s;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string q;
    cin >> q;
    bool f = true;
    int br = 0;
    for (int j = 0; j < s.size(); j++) {
      if (br >= q.size()) {
        if (s[j] != '*' || j != (s.size() - 1)) f = false;
        break;
      }
      if (s[j] != '*' && s[j] != '?' && s[j] != q[br])
        f = false;
      else if (s[j] == '?' && l[q[br] - 'a'] == false)
        f = false;
      else if (s[j] == '*') {
        int t = j;
        int s1 = s.size() - j, s2 = q.size() - j, k = 0;
        for (k = j; k < j + 1 + s2 - s1 && k < q.size(); k++) {
          if (l[q[k] - 'a'] == true) f = false;
        }
        br = k - 1;
      }
      br++;
    }
    if (f == true && br >= q.size())
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
