#include <bits/stdc++.h>
using namespace std;
static const double EPS = 1e-9;
template <class T>
bool INRANGE(T x, T a, T b) {
  return a <= x && x <= b;
}
template <class T>
void amin(T& a, T v) {
  if (a > v) a = v;
}
template <class T>
void amax(T& a, T v) {
  if (a < v) a = v;
}
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) {
  return fabs(x - y) <= EPS * max(1.0, max(fabs(x), fabs(y)));
}
double SQSUM(double x, double y) { return x * x + y * y; }
bool isGood[256];
string mihon;
bool kome;
int pre;
int suf;
int len;
bool checkLetter(int i, const string& s) {
  if (mihon[i] == '?') {
    if (!isGood[s[i]]) return false;
  } else {
    if (mihon[i] != s[i]) return false;
  }
  return true;
}
bool checkLetter2(int i, int zure, const string& s) {
  if (mihon[i - zure] == '?') {
    if (!isGood[s[i]]) return false;
  } else {
    if (mihon[i - zure] != s[i]) return false;
  }
  return true;
}
bool check(const string& s) {
  if (kome) {
    const int komelen = ((int)(s).size()) - pre - suf;
    if (komelen < 0) return false;
    for (int i = 0; i < pre; i++) {
      if (!checkLetter(i, s)) return false;
    }
    for (int i = pre; i < pre + komelen; i++) {
      if (isGood[s[i]]) return false;
    }
    for (int i = pre + komelen; i < ((int)(s).size()); i++) {
      if (!checkLetter2(i, komelen - 1, s)) return false;
    }
  } else {
    if (((int)(s).size()) != len) return false;
    for (int i = 0; i < len; ++i) {
      if (!checkLetter(i, s)) return false;
    }
  }
  return true;
}
int main() {
  string good;
  cin >> good >> mihon;
  for (int i = 0; i < ((int)(good).size()); ++i) {
    isGood[good[i]] = true;
  }
  len = ((int)(mihon).size());
  kome = false;
  for (int i = 0; i < len; i++) {
    if (mihon[i] == '*') {
      kome = true;
      pre = i;
      suf = len - pre - 1;
      break;
    }
  }
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s;
    cin >> s;
    if (check(s)) {
      cout << "YES" << endl;
    } else {
      cout << "NO" << endl;
    }
  }
  return 0;
}
