def count_safe_challenges(N, challenges, Q, heroes): from bisect import bisect_right from collections import defaultdict challenges.sort() difficulties = [ch[0] for ch in challenges] type_dict = defaultdict(list) for difficulty, ctype in challenges: type_dict[ctype].append(difficulty) for ctype in type_dict: type_dict[ctype].sort() results = [] for strength, weakness in heroes: total_count = bisect_right(difficulties, strength) if weakness in type_dict: weak_difficulties = type_dict[weakness] weak_count = bisect_right(weak_difficulties, strength) total_count -= weak_count results.append(total_count) return results if __name__ == "__main__": N = int(input().strip()) challenges = [tuple(map(int, input().strip().split())) for _ in range(N)] Q = int(input().strip()) heroes = [tuple(map(int, input().strip().split())) for _ in range(Q)] results = count_safe_challenges(N, challenges, Q, heroes) for res in results: print(res)