#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 5;
char tmp[30], pat[M], s[M];
bool cnt[30];
int main() {
  memset(cnt, 0, sizeof(cnt));
  scanf("%s %s", tmp, pat);
  int lt = strlen(tmp), lp = strlen(pat);
  for (int i = 0; i < lt; ++i) {
    cnt[tmp[i] - 'a'] = true;
  }
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    bool flag = false;
    scanf("%s", s);
    int len = strlen(s), pos = 0, j;
    for (j = 0; j < lp; ++j) {
      if (s[pos] == pat[j])
        ++pos;
      else if (pat[j] == '?') {
        if (cnt[s[pos] - 'a'])
          ++pos;
        else
          break;
      } else if (pat[j] == '*') {
        if (len == lp) {
          if (cnt[s[j] - 'a'])
            break;
          else
            ++pos;
        } else if (len + 1 == lp)
          continue;
        else if (len + 1 < lp)
          break;
        else {
          int k;
          for (k = j; k <= j + (len - lp); ++k) {
            if (cnt[s[k] - 'a']) {
              break;
            }
          }
          if (k != j + len - lp + 1) {
            break;
          } else
            pos = k;
        }
      } else {
        break;
      }
    }
    if (j == lp && pos == len) flag = true;
    if (flag)
      puts("YES");
    else
      puts("NO");
  }
  return 0;
}
