#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  string good;
  cin >> good;
  string pattern;
  cin >> pattern;
  set<char> s;
  for (int i = 0; i < good.length(); i++) s.insert(good[i]);
  int lenp = 0;
  for (int i = 0; i < pattern.length(); i++) {
    if (pattern[i] != '*') lenp++;
  }
  int cnt_star = (int)pattern.length() - lenp;
  int q;
  cin >> q;
  while (q--) {
    string query;
    cin >> query;
    int lenq = query.length();
    if (lenp > lenq) {
      cout << "NO" << endl;
    } else if (lenp == lenq) {
      bool ok = 0;
      string temp;
      for (int i = 0; i < pattern.length(); i++) {
        if (pattern[i] != '*') temp += pattern[i];
      }
      for (int i = 0; i < lenq; i++) {
        if (temp[i] == query[i])
          continue;
        else if (temp[i] != query[i] && temp[i] != '?') {
          ok = 1;
          break;
        }
        if (temp[i] == '?') {
          if (s.count(query[i]))
            continue;
          else {
            ok = 1;
            break;
          }
        }
      }
      if (ok)
        cout << "NO" << endl;
      else
        cout << "YES" << endl;
    } else if (lenp < lenq) {
      int a = lenq - lenp;
      bool ok = 0;
      int t = 0, i = 0;
      while (i < lenq) {
        if (pattern[t] == query[i]) {
          t++;
          i++;
          continue;
        } else if (pattern[t] != query[i] && pattern[t] != '?' &&
                   pattern[t] != '*') {
          ok = 1;
          break;
        }
        if (pattern[t] == '?') {
          if (s.count(query[i])) {
            t++;
            i++;
            continue;
          } else {
            ok = 1;
            break;
          }
        } else if (pattern[t] == '*') {
          for (int j = i; j < (i + a); j++) {
            if (s.count(query[j])) {
              ok = 1;
              break;
            }
          }
          if (ok) break;
          i += a;
        }
        t++;
      }
      if (ok)
        cout << "NO" << endl;
      else
        cout << "YES" << endl;
    }
  }
  return 0;
}
