from collections import defaultdict n = int(input()) challenges = [] for _ in range(n): a, b = map(int, input().split()) challenges.append((a, b)) by_difficulty = defaultdict(lambda: defaultdict(int)) for difficulty, challenge_type in challenges: by_difficulty[difficulty][challenge_type] += 1 q = int(input()) for _ in range(q): x, y = map(int, input().split()) if x == 1000000000: print(0) continue count = 0 for difficulty, types_dict in by_difficulty.items(): if difficulty <= x: count += sum(types_dict.values()) - types_dict.get(y, 0) print(count)