#include <bits/stdc++.h>
bool is_good(const std::string& string, const std::string& template_str,
             const std::vector<bool>& good_chars_array, bool has_asterix) {
  if (!has_asterix) {
    if (template_str.size() != string.size()) {
      return false;
    }
    for (int i = 0; i < string.size(); ++i) {
      if (template_str[i] != '?') {
        if (template_str[i] != string[i]) {
          return false;
        }
      } else {
        if (!good_chars_array[string[i] - 'a']) {
          return false;
        }
      }
    }
    return true;
  }
  int asterix_length = string.size() - template_str.size() + 1;
  if (asterix_length < 0) {
    return false;
  }
  bool we_are_before_asterix = true;
  for (int i = 0; i < template_str.size(); ++i) {
    if (template_str[i] == '*') {
      we_are_before_asterix = false;
      for (int j = i; j < i + asterix_length; ++j) {
        if (good_chars_array[string[j] - 'a']) {
          return false;
        }
      }
    } else {
      int position_in_string;
      if (we_are_before_asterix) {
        position_in_string = i;
      } else {
        position_in_string = i + asterix_length - 1;
      }
      if (template_str[i] != '?') {
        if (template_str[i] != string[position_in_string]) {
          return false;
        }
      } else {
        if (!good_chars_array[string[position_in_string] - 'a']) {
          return false;
        }
      }
    }
  }
  return true;
}
void solve(std::istream& input, std::ostream& output) {
  std::string good_chars;
  input >> good_chars;
  std::string template_str;
  input >> template_str;
  int num_questions;
  input >> num_questions;
  std::vector<bool> good_chars_array(26, false);
  for (int i = 0; i < good_chars.size(); ++i) {
    int now = good_chars[i] - 'a';
    good_chars_array[now] = true;
  }
  bool has_asterix = false;
  for (int i = 0; i < template_str.size(); ++i) {
    if (template_str[i] == '*') {
      has_asterix = true;
    }
  }
  char now_c[100005];
  input.getline(now_c, 100005);
  std::string now;
  for (int i = 0; i < num_questions; ++i) {
    input.getline(now_c, 100005);
    if (is_good(std::string(now_c), template_str, good_chars_array,
                has_asterix)) {
      std::cout << "YES\n";
    } else {
      std::cout << "NO\n";
    }
  }
}
int main() {
  std::ifstream ifs("input.txt", std::ifstream::in);
  std::ios_base::sync_with_stdio(false);
  ifs.tie(nullptr);
  std::cin.tie(nullptr);
  solve(std::cin, std::cout);
}
