#include <bits/stdc++.h>
using namespace std;
template <class T>
T gcd(T a, T b) {
  if (!b) return a;
  return gcd(b, a % b);
}
template <class T>
T power(T a, T b) {
  T res(1);
  while (b) {
    if (b & 1) res = res * a;
    a = a * a;
    b >>= 1;
  }
  return res;
}
template <class T>
T powerM(T a, T b, T mod) {
  T res(1);
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
const int infi = 2147483647;
const long long infl = 9223372036854775807;
int n, m, k, T;
char s[111000], ch[30], pa[111000];
bool is[30];
int main() {
  scanf("%s %s", ch, pa);
  memset(is, 0, sizeof is);
  int ff = -1;
  T = strlen(pa);
  for (int i = 0; i < (T); ++i)
    if (pa[i] == '*') ff = i;
  for (int i = 0; i < (strlen(ch)); ++i) is[ch[i] - 'a'] = 1;
  scanf("%d", &n);
  while (n--) {
    scanf("%s", s);
    m = strlen(s);
    int fl = 0;
    for (int i = 0; i < (m); ++i)
      if (!is[s[i] - 'a']) fl = 1;
    if (ff == -1) {
      if (m != T) {
        puts("NO");
        continue;
      }
      int i = 0, ok = 1;
      while (i < m) {
        if (pa[i] == '?') {
          if (!is[s[i] - 'a']) ok = 0;
        } else {
          if (s[i] != pa[i]) ok = 0;
        }
        i++;
      }
      if (ok)
        puts("YES");
      else
        puts("NO");
    } else {
      if (m < T - 1) {
        puts("NO");
        continue;
      }
      int i = 0, ok = 1;
      while (i < ff && i < m) {
        if (pa[i] == '?') {
          if (!is[s[i] - 'a']) ok = 0;
        } else {
          if (s[i] != pa[i]) ok = 0;
        }
        i++;
      }
      int j = T - 1, l = m - 1;
      while (j > ff && l >= i) {
        if (pa[j] == '?') {
          if (!is[s[l] - 'a']) ok = 0;
        } else {
          if (s[l] != pa[j]) ok = 0;
        }
        j--;
        l--;
      }
      while (i <= l) {
        if (is[s[i] - 'a']) ok = 0;
        i++;
      }
      if (ok)
        puts("YES");
      else
        puts("NO");
    }
  }
  return 0;
}
