import collections def read_data(): N = int(input()) BCs = [] for n in range(N): b, c = map(int, input().split()) BCs.append((b, c)) M = int(input()) Es = [0] * N for m in range(M): d, e = map(int, input().split()) Es[d] += (1 << e) return N, BCs, Es def solve(N, BCs, Es): dp = collections.defaultdict(int) dp[0] = 0 for i in range(N): b, c = BCs[i] mask = Es[i] new_dp = collections.defaultdict(int) idx = 1 << i for key, val in dp.items(): tour = key & idx new_key = key - tour new_val = val if tour else (val + c) if new_val > new_dp[new_key]: new_dp[new_key] = new_val new_key |= mask new_val = val + b if new_val > new_dp[new_key]: new_dp[new_key] = new_val dp = new_dp return dp[0] N, BCs, Es = read_data() print(solve(N, BCs, Es))