#include <bits/stdc++.h>
using namespace std;
bool valid(string s, string query, string good_letters, string bad_letters) {
  if (query.length() == s.length() - 1 && s.find('*') != string::npos) {
    for (int i = 0, m = s.find('*'); i < m; i++) {
      if (s[i] != '?' && query[i] != s[i] ||
          s[i] == '?' && good_letters.find(query[i]) == string::npos) {
        return false;
      }
    }
    for (int i = s.find('*') + 1; i < s.length(); i++) {
      if (s[i] != '?' && query[i - 1] != s[i] ||
          s[i] == '?' && good_letters.find(query[i - 1]) == string::npos) {
        return false;
      }
    }
    return true;
  }
  if (query.length() >= s.length() && s.find('*') != string::npos) {
    for (int i = 0, m = s.find('*'); i < m; i++) {
      if (s[i] != '?' && query[i] != s[i] ||
          s[i] == '?' && good_letters.find(query[i]) == string::npos) {
        return false;
      }
    }
    for (int i = s.find('*'), m = i + query.length() - s.length(); i <= m;
         i++) {
      if (bad_letters.find(query[i]) == string::npos) {
        return false;
      }
    }
    for (int i = s.find('*') + 1; i < s.length(); i++) {
      if (s[i] != '?' && query[i + query.length() - s.length()] != s[i] ||
          s[i] == '?' &&
              good_letters.find(query[i + query.length() - s.length()]) ==
                  string::npos) {
        return false;
      }
    }
    return true;
  }
  if (query.length() == s.length()) {
    for (int i = 0; i < s.length(); i++) {
      if (s[i] != '?' && query[i] != s[i] ||
          s[i] == '?' && good_letters.find(query[i]) == string::npos) {
        return false;
      }
    }
    return true;
  }
  return false;
}
int main() {
  string good_letters, bad_letters, s;
  cin >> good_letters >> s;
  for (char c = 'a'; c <= 'z'; c++) {
    if (good_letters.find(c) == string::npos) {
      bad_letters.push_back(c);
    }
  }
  int n;
  for (cin >> n; n; n--) {
    string query;
    cin >> query;
    if (valid(s, query, good_letters, bad_letters)) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
}
