public static void main ( String [ ] args ) {
  Scanner sc = new Scanner ( System . in ) ;
  String s = sc . next ( ) . trim ( ) ;
  ArrayList < String > ch = new ArrayList < String > ( ) ;
  for ( int i = 0 ;
  i < s . length ( ) ;
  ++ i ) ch . add ( s . substring ( i , i + 1 ) ) ;
  String p = sc . next ( ) . trim ( ) ;
  int n = sc . nextInt ( ) ;
  for ( int i = 0 ;
  i < n ;
  ++ i ) {
    String t = sc . next ( ) . trim ( ) ;
    int jt = 0 ;
    int k = p . length ( ) , m = t . length ( ) ;
    String ans = "" ;
    for ( int jp = 0 ;
    jp < k && ans . length ( ) == 0 ;
    ++ jp ) {
      String pa = p . substring ( jp , jp + 1 ) ;
      String ta = jt < m ? t . substring ( jt , jt + 1 ) : "BLERGH" ;
      if ( pa . equals ( "?" ) ) {
        if ( jt < m ) {
          if ( ! ch . contains ( ta ) ) ans = "NO" ;
          else {
            ++ jt ;
          }
        }
        else ans = "NO" ;
      }
      else if ( pa . equals ( "*" ) ) {
        for ( int y = 0 ;
        y < m - k + 1 ;
        ++ y ) {
          if ( ! ch . contains ( t . substring ( jt , jt + 1 ) ) ) ++ jt ;
          else {
            ans = "NO" ;
          }
        }
      }
      else {
        if ( jt < m ) {
          if ( ! pa . equals ( ta ) ) ans = "NO" ;
          else {
            ++ jt ;
          }
        }
        else ans = "NO" ;
      }
    }
    if ( jt == m && ans . length ( ) == 0 ) ans = "YES" ;
    else ans = "NO" ;
    System . out . println ( ans ) ;
  }
  sc . close ( ) ;
}

public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; String s = sc . next ( ) . trim ( ) ; ArrayList < String > ch = new ArrayList < String > ( ) ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) ch . add ( s . substring ( i , i + 1 ) ) ; String p = sc . next ( ) . trim ( ) ; int n = sc . nextInt ( ) ; for ( int i = 0 ; i < n ; ++ i ) { String t = sc . next ( ) . trim ( ) ; int jt = 0 ; int k = p . length ( ) , m = t . length ( ) ; String ans = " " ; for ( int jp = 0 ; jp < k && ans . length ( ) == 0 ; ++ jp ) { String pa = p . substring ( jp , jp + 1 ) ; String ta = jt < m ? t . substring ( jt , jt + 1 ) : " BLERGH " ; if ( pa . equals ( " ? " ) ) { if ( jt < m ) { if ( ! ch . contains ( ta ) ) ans = " NO " ; else { ++ jt ; } } else ans = " NO " ; } else if ( pa . equals ( " * " ) ) { for ( int y = 0 ; y < m - k + 1 ; ++ y ) { if ( ! ch . contains ( t . substring ( jt , jt + 1 ) ) ) ++ jt ; else { ans = " NO " ; } } } else { if ( jt < m ) { if ( ! pa . equals ( ta ) ) ans = " NO " ; else { ++ jt ; } } else ans = " NO " ; } } if ( jt == m && ans . length ( ) == 0 ) ans = " YES " ; else ans = " NO " ; System . out . println ( ans ) ; } sc . close ( ) ; }
(method_declaration (modifiers) type: (void_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (array_type element: (type_identifier) dimensions: (dimensions)) name: (identifier))) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (type_identifier) arguments: (argument_list (field_access object: (identifier) field: (identifier)))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (generic_type (type_identifier) (type_arguments (type_identifier))) declarator: (variable_declarator name: (identifier) value: (object_creation_expression type: (generic_type (type_identifier) (type_arguments (type_identifier))) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) update: (update_expression (identifier)) body: (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (identifier)) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list))) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (string_literal))) (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (local_variable_declaration type: (type_identifier) declarator: (variable_declarator name: (identifier) value: (ternary_expression condition: (binary_expression left: (identifier) right: (identifier)) consequence: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal)))) alternative: (string_literal)))) (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (string_literal)))) consequence: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (if_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) consequence: (expression_statement (assignment_expression left: (identifier) right: (string_literal))) alternative: (block (expression_statement (update_expression (identifier)))))) alternative: (expression_statement (assignment_expression left: (identifier) right: (string_literal))))) alternative: (if_statement condition: (parenthesized_expression (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (string_literal)))) consequence: (block (for_statement init: (local_variable_declaration type: (integral_type) declarator: (variable_declarator name: (identifier) value: (decimal_integer_literal))) condition: (binary_expression left: (identifier) right: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (decimal_integer_literal))) update: (update_expression (identifier)) body: (block (if_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier) (binary_expression left: (identifier) right: (decimal_integer_literal)))))))) consequence: (expression_statement (update_expression (identifier))) alternative: (block (expression_statement (assignment_expression left: (identifier) right: (string_literal)))))))) alternative: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) consequence: (block (if_statement condition: (parenthesized_expression (unary_expression operand: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list (identifier))))) consequence: (expression_statement (assignment_expression left: (identifier) right: (string_literal))) alternative: (block (expression_statement (update_expression (identifier)))))) alternative: (expression_statement (assignment_expression left: (identifier) right: (string_literal))))))))) (if_statement condition: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (binary_expression left: (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)) right: (decimal_integer_literal)))) consequence: (expression_statement (assignment_expression left: (identifier) right: (string_literal))) alternative: (expression_statement (assignment_expression left: (identifier) right: (string_literal)))) (expression_statement (method_invocation object: (field_access object: (identifier) field: (identifier)) name: (identifier) arguments: (argument_list (identifier)))))) (expression_statement (method_invocation object: (identifier) name: (identifier) arguments: (argument_list)))))