• + 2 comments

    Your solution is very inefficient.

    Here I suggest you functional approach, better efficiency and readability.

    Scanner sc = new Scanner(System.in);
    
    Map<String, Long> counterMap = Stream.generate(sc::next)
        .limit(sc.nextInt())
        .collect(groupingBy(identity(), counting()));
    
    Stream.generate(sc::next)
        .limit(sc.nextInt())
        .map(query -> counterMap.getOrDefault(query, 0L))
        .forEach(System.out::println);
    

    P.S. imports are omitted.