You are participating in a contest which has $11$ problems (numbered $1$ through $11$). The first eight problems (i.e. problems $1, 2, \ldots, 8$) are scorable, while the last three problems ($9$, $10$ and $11$) are non-scorable ― this means that any submissions you make on any of these problems do not affect your total score.
Your total score is the sum of your best scores for all scorable problems. That is, for each scorable problem, you look at the scores of all submissions you made on that problem and take the maximum of these scores (or $0$ if you didn't make any submissions on that problem); the total score is the sum of the maximum scores you took.
You know the results of all submissions you made. Calculate your total score.

-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains a single integer $N$ denoting the number of submissions you made.
- $N$ lines follow. For each $i$ ($1 \le i \le N$), the $i$-th of these lines contains two space-separated integers $p_i$ and $s_i$, denoting that your $i$-th submission was on problem $p_i$ and it received a score $s_i$.

-----Output-----
For each test case, print a single line containing one integer ― your total score.

-----Constraints-----
- $1 \le T \le 10$
- $1 \le N \le 1,000$
- $1 \le p_i \le 11$ for each valid $i$
- $0 \le s_i \le 100$ for each valid $i$

-----Subtasks-----
Subtask #1 (15 points): all submissions are on the same problem, i.e. $p_1 = p_2 = \ldots = p_N$
Subtask #2 (15 points): there is at most one submission made on each problem, i.e. $p_i \neq p_j$ for each valid $i, j$ ($i \neq j$)
Subtask #3 (70 points): original constraints

-----Example Input-----
2
5
2 45
9 100
8 0
2 15
8 90
1
11 1

-----Example Output-----
135
0

-----Explanation-----
Example case 1: The scorable problems with at least one submission are problems $2$ and $8$. For problem $2$, there are two submissions and the maximum score among them is $45$. For problem $8$, there are also two submissions and the maximum score is $90$. Hence, the total score is $45 + 90 = 135$.
Example case 2: No scorable problem is attempted, so the total score is $0$.

Difficulty: EASY
Tags: Data structures

Implementation
Skill types: Data structures

Starter code:

Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	dict1 = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0}
	for j in range(n):
		(a, b) = list(map(int, input().strip().split()))
		if a == 9 or a == 10 or a == 11:
			pass
		else:
			dict1[a] = max(dict1[a], b)
	sum1 = 0
	for i in dict1.values():
		sum1 += i
	print(sum1)


for i in range(int(input())):
	arr = [0] * 8
	for j in range(int(input())):
		(a, b) = map(int, input().split())
		if a in range(1, 9):
			arr[a - 1] = max(b, arr[a - 1])
	print(sum(arr))


for _ in range(int(input())):
	d = {}
	n = int(input())
	for i in range(n):
		(k, v) = map(int, input().split())
		if k <= 8:
			if k not in d.keys():
				d[k] = v
			elif d[k] < v:
				d[k] = v
	print(sum(d.values()))


for i in range(int(input())):
	result = dict()
	submissions = int(input())
	for j in range(submissions):
		(problem, score) = map(int, input().split(' '))
		if problem < 9:
			try:
				if result[problem] < score:
					result[problem] = score
			except:
				result[problem] = score
	total_marks = sum(result.values())
	print(total_marks)


for i in range(int(input())):
	n = int(input())
	d = {}
	for i in range(n):
		(a, b) = map(int, input().split())
		if a in d:
			d[a].append(b)
		else:
			d[a] = [b]
	c = 0
	for i in range(1, 9):
		if i in d:
			c += max(d[i])
	print(c)


n = int(input())
while n > 0:
	t = int(input())
	a = []
	for i in range(8):
		a.append(0)
	for i in range(t):
		(b, c) = [int(x) for x in input().split()]
		if b <= 8 and c > a[b - 1]:
			a[b - 1] = c
	print(sum(a))
	n = n - 1


for _ in range(int(input())):
	n = int(input())
	r = {}
	for i in range(1, 12):
		r[i] = 0
	for i in range(n):
		(p, s) = map(int, input().split())
		if r[p] < s:
			r[p] = s
		else:
			continue
	c = 0
	for i in range(1, 9):
		c += r[i]
	print(c)


a = int(input())
for i in range(a):
	b = int(input())
	e = {}
	for j in range(b):
		(c, d) = map(int, input().split())
		if c not in (9, 10, 11):
			if c not in e:
				e[c] = d
			elif e.get(c) < d:
				e[c] = d
	print(sum(e.values()))


rii = lambda : list(map(int, input().strip().split(' ')))
ril = lambda : list(map(int, input().strip().split(' ')))
ri = lambda : int(input().strip())
rs = lambda : eval(input())
T = ri()
for _ in range(T):
	N = ri()
	l = [0 for i in range(8)]
	for i in range(N):
		(nb, S) = rii()
		if nb < 9:
			l[nb - 1] = max(S, l[nb - 1])
	print(sum(l))


for t in range(int(input())):
	n = int(input())
	l = [0] * 8
	for i in range(n):
		(p, s) = list(map(int, input().split()))
		if p <= 8 and l[p - 1] < s:
			l[p - 1] = s
	print(sum(l))


for t in range(int(input())):
	num = int(input())
	dt = dict()
	for i in range(num):
		(prob, score) = map(int, input().split())
		if prob in range(1, 9):
			if prob not in dt:
				dt[prob] = score
			elif score > dt[prob]:
				dt[prob] = score
	print(sum(dt.values()))


for _ in range(int(input())):
	n = int(input())
	a = []
	total = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		a.append([x, y])
	a.sort()
	d = {i[0]: i[1] for i in a}
	for i in d.keys():
		if i <= 8:
			total += d[i]
	print(total)


h = int(input())
for i in range(h):
	a = int(input())
	sum1 = sum2 = sum3 = sum4 = sum5 = sum6 = sum7 = sum8 = 0
	for j in range(a):
		(b, c) = list(map(int, input().split()))
		if b == 1:
			if c > sum1:
				sum1 = c
		elif b == 2:
			if c > sum2:
				sum2 = c
		elif b == 3:
			if c > sum3:
				sum3 = c
		elif b == 4:
			if c > sum4:
				sum4 = c
		elif b == 5:
			if c > sum5:
				sum5 = c
		elif b == 6:
			if c > sum6:
				sum6 = c
		elif b == 7:
			if c > sum7:
				sum7 = c
		elif b == 8:
			if c > sum8:
				sum8 = c
	print(sum1 + sum2 + sum3 + sum4 + sum5 + sum6 + sum7 + sum8)


t = int(input())
for i in range(t):
	n = int(input())
	dict_a = {}
	for i in range(n):
		(a, b) = map(int, input().split())
		if a < 9:
			if a not in dict_a:
				dict_a[a] = b
			elif b > dict_a[a]:
				dict_a[a] = b
	total = 0
	for i in dict_a.values():
		total += i
	print(total)


for _ in range(int(input())):
	n = int(input())
	ls = []
	score = 0
	for i in range(n):
		(x, y) = map(int, input().split())
		ls.append([y, x])
	ls.sort()
	dic = {i[1]: i[0] for i in ls}
	for i in dic.keys():
		if i <= 8:
			score += dic[i]
	print(score)


for _ in range(int(input())):
	n = int(input())
	d = dict()
	c = 0
	l = []
	for i in range(n):
		(x, y) = map(int, input().split())
		l.append([y, x])
	l.sort()
	d = {i[1]: i[0] for i in l}
	for i in d.keys():
		if i <= 8:
			c += d[i]
	print(c)


for _ in range(int(input())):
	n = int(input())
	l = [0, 0, 0, 0, 0, 0, 0, 0, 0]
	for i in range(n):
		(s, p) = map(int, input().split())
		if s <= 8:
			if l[s - 1] < p:
				l[s - 1] = p
	print(sum(l))


for _ in range(int(input())):
	score = 0
	d = {}
	for x in range(int(input())):
		(p, s) = map(int, input().split())
		if p in d:
			if d[p] < s:
				d[p] = s
		else:
			d[p] = s
	for (key, value) in d.items():
		if key < 9:
			score = score + value
	print(score)


t = int(input())
for i in range(t):
	n = int(input())
	A = [0] * 9
	for j in range(n):
		(a, b) = list(map(int, input().split()))
		if a < 9 and A[a] < b:
			A[a] = b
	print(sum(A))


for i in range(int(input())):
	n = int(input())
	d = {}
	for i in range(n):
		(x, y) = list(map(int, input().split()))
		if x != 9 and x != 10 and (x != 11):
			if x not in d:
				d[x] = y
			else:
				d[x] = max(d[x], y)
	print(sum(d.values()))


for _ in range(int(input())):
	n = int(input())
	li = [0 for i in range(12)]
	for _ in range(n):
		(p, s) = map(int, input().split())
		if li[p] < s:
			li[p] = s
	print(sum(li[1:9]))


for _ in range(int(input())):
	d = {}
	for i in range(1, 12):
		d[i] = 0
	for _ in range(int(input())):
		(n, p) = map(int, input().split())
		if d[n] < p:
			d[n] = p
		else:
			pass
	c = 0
	for i in range(1, 9):
		c += d[i]
	print(c)


t = int(input())
for i in range(t):
	n = int(input())
	a = [0]
	b = [0]
	c = [0]
	d = [0]
	e = [0]
	f = [0]
	g = [0]
	h = [0]
	for j in range(n):
		(q, s) = list(map(int, input().split()))
		if q < 9:
			if q == 1:
				a.append(s)
			elif q == 2:
				b.append(s)
			elif q == 3:
				c.append(s)
			elif q == 4:
				d.append(s)
			elif q == 5:
				e.append(s)
			elif q == 6:
				f.append(s)
			elif q == 7:
				g.append(s)
			elif q == 8:
				h.append(s)
	print(max(a) + max(b) + max(c) + max(d) + max(e) + max(f) + max(g) + max(h))


for i in range(int(input())):
	score = 0
	a = []
	for j in range(int(input())):
		t = input().split()
		a.append(t)
	d = {}
	for t in a:
		if int(t[0]) == 9 or int(t[0]) == 10 or int(t[0]) == 11:
			score += 0
		elif t[0] in d:
			d[t[0]] = max(int(t[1]), d[t[0]])
		else:
			d[t[0]] = int(t[1])
	for key in d:
		score += d[key]
	print(score)


for T in range(int(input())):
	n = int(input())
	s = [0] * 11
	score = 0
	for i in range(n):
		(p, sc) = map(int, input().split())
		if sc > s[p - 1]:
			s[p - 1] = sc
	for i in range(8):
		score += s[i]
	print(score)


for t in range(int(input())):
	n = int(input())
	l = [0] * 11
	for i in range(n):
		(x, y) = map(int, input().split())
		if l[x - 1] < y:
			l[x - 1] = y
	print(sum(l[0:8]))


for i in range(int(input())):
	result = [0] * 8
	submissions = int(input())
	for j in range(submissions):
		(problem, score) = map(int, input().split(' '))
		if problem < 9:
			if result[problem - 1] < score:
				result[problem - 1] = score
	print(sum(result))


import collections
T = int(input())
while T > 0:
	N = int(input())
	a = []
	for i in range(8):
		a.append(0)
	for i in range(N):
		(b, c) = [int(i) for i in input().split()]
		if b <= 8 and c > a[b - 1]:
			a[b - 1] = c
	print(sum(a))
	T -= 1


for t in range(int(input())):
	n = int(input())
	dct = dict()
	for i in range(n):
		(x, y) = map(int, input().split())
		if x not in dct:
			dct[x] = y
		elif y > dct[x]:
			dct[x] = y
	sum = 0
	for d in dct:
		if d > 8:
			continue
		else:
			sum += dct[d]
	print(sum)


t = int(input())
for _ in range(t):
	n = int(input())
	best_scores = {i: 0 for i in range(1, 9)}
	for a in range(n):
		(p, s) = map(int, input().split())
		if p <= 8:
			best_scores[p] = max(best_scores[p], s)
	print(sum(best_scores.values()))


for t in range(int(input())):
	n = int(input())
	d = dict()
	for i in range(n):
		(p, s) = map(int, input().split())
		if p in range(1, 9):
			if p not in d:
				d[p] = s
			elif s > d[p]:
				d[p] = s
	print(sum(d.values()))


for _ in range(int(input())):
	n = int(input())
	l = []
	d = dict()
	for i in range(n):
		l = [int(i) for i in input().split()]
		if l[0] in [1, 2, 3, 4, 5, 6, 7, 8]:
			if l[0] in list(d.keys()):
				if d[l[0]] < l[1]:
					d[l[0]] = l[1]
			else:
				d[l[0]] = l[1]
	s = 0
	for (i, j) in list(d.items()):
		s = s + j
	print(s)


for _ in range(int(input())):
	n = int(input())
	fc = [0] * 9
	for i in range(n):
		(n, m) = map(int, input().split())
		if n < len(fc):
			if fc[n] < m:
				fc[n] = m
	print(sum(fc))


t = int(input())
for i in range(t):
	n = int(input())
	l = []
	c = 0
	for j in range(n):
		(x, y) = map(int, input().split())
		l.append([y, x])
	l.sort()
	d = {i[1]: i[0] for i in l}
	for k in d.keys():
		if k <= 8:
			c = c + d[k]
	print(c)


for _ in range(int(input())):
	n = int(input())
	d = {}
	for i in range(n):
		(m, n) = map(int, input().split())
		if m >= 1 and m <= 8:
			if m not in d:
				d[m] = n
			elif d[m] < n:
				d[m] = n
	print(sum(d.values()))


for _ in range(int(input())):
	n = int(input())
	d = {1: [], 2: [], 3: [], 4: [], 5: [], 6: [], 7: [], 8: []}
	l = []
	su = 0
	for i in range(n):
		(p, s) = list(map(int, input().split()))
		if p <= 8:
			d[p] = d[p] + [s]
	for j in d:
		if len(d[j]) != 0:
			su = su + max(d[j])
	print(su)


for _ in range(int(input())):
	a = int(input())
	b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	c = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	for i in range(a):
		(d, e) = map(int, input().split())
		for j in range(len(b)):
			if d - 1 == j:
				if e > c[j]:
					c[j] = e
				break
	c[8] = c[9] = c[10] = 0
	print(sum(c))


for i in range(int(input())):
	d1 = {}
	n = int(input())
	for j in range(1, 12):
		d1[j] = 0
	for k in range(n):
		(p, s) = map(int, input().split())
		if d1[p] < s:
			d1[p] = s
		else:
			continue
	result = 0
	for l in range(1, 9):
		result += d1[l]
	print(result)


t = int(input())
for i in range(t):
	x = dict()
	a = int(input())
	for j in range(a):
		(p, s) = map(int, input().split())
		if p < 9:
			try:
				if x[p] < s:
					x[p] = s
			except:
				x[p] = s
	total = sum(x.values())
	print(total)


for _ in range(int(input())):
	Score = [0] * 8
	for _ in range(int(input())):
		(P, S) = list(map(int, input().split()))
		if P >= 1 and P <= 8:
			if Score[P - 1] < S:
				Score[P - 1] = S
	print(sum(Score))


for i in range(int(input())):
	n = int(input())
	l = {}
	s = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		if a == 9 or a == 10 or a == 11:
			continue
		elif a in l:
			z = max(l[a], b)
			l[a] = z
		else:
			l[a] = b
	for i in l:
		s = s + l[i]
	print(s)


T = int(input())
for i in range(T):
	n = int(input())
	l = [0] * 11
	for i in range(n):
		(X, Y) = map(int, input().split())
		if X < 9 and l[X - 1] < Y:
			l[X - 1] = Y
	print(sum(l))


for i in range(int(input())):
	d = dict()
	n = int(input())
	for j in range(n):
		(p, s) = map(int, input().split(' '))
		if p < 9:
			try:
				if d[p] < s:
					d[p] = s
			except:
				d[p] = s
	tot = sum(d.values())
	print(tot)


for _ in range(int(input())):
	n = int(input())
	arr = []
	for i in range(11):
		arr.append(0)
	for i in range(n):
		(p, s) = list(map(int, input().split()))
		if p <= 8:
			arr[p - 1] = max(arr[p - 1], s)
	print(sum(arr))


for _ in range(int(input())):
	n = int(input())
	d = {}
	for i in range(n):
		(x, y) = map(int, input().split())
		if x <= 8:
			if x in d:
				y = max(d[x], y)
			d[x] = y
	a = 0
	for x in d.values():
		a += x
	print(a)


t = int(input())
for i in range(t):
	d = {}
	n = int(input())
	for j in range(n):
		(x, y) = map(int, input().split())
		if x in d:
			d[x].append(y)
		else:
			d[x] = [y]
	sum = 0
	prob = [1, 2, 3, 4, 5, 6, 7, 8]
	for j in d:
		if j in prob:
			sum += max(d[j])
	print(sum)


m = int(input())
for i in range(m):
	n = int(input())
	d = {}
	for i in range(n):
		(k, l) = list(map(int, input().split()))
		if k <= 8:
			if k in d:
				d[k] = max(l, d[k])
			else:
				d[k] = l
	print(sum(d.values()))


for _ in range(int(input())):
	n = int(input())
	hsh = [0] * 9
	for _ in range(n):
		(q, s) = map(int, input().split())
		if q in range(1, 9):
			if hsh[q] < s:
				hsh[q] = s
	print(sum(hsh))


for i in range(0, int(input())):
	n = int(input())
	l = []
	for j in range(n):
		l.append(list(map(int, input().split())))
	a = [0, 0, 0, 0, 0, 0, 0, 0]
	for k in range(0, len(l)):
		t = l[k][0]
		if t < 9:
			if l[k][1] >= a[t - 1]:
				a[t - 1] = l[k][1]
			else:
				continue
		else:
			continue
	print(sum(a))


p = int(input())
for z in range(p):
	n = int(input())
	a = []
	for i in range(8):
		a.append(0)
	for i in range(n):
		(x, y) = list(map(int, input().split()))
		if x <= 8 and y > a[x - 1]:
			a[x - 1] = y
	print(sum(a))


z = int(input())
for i in range(z):
	k = [0] * 8
	s = int(input())
	for j in range(s):
		(pos, sub) = map(int, input().split(' '))
		if pos < 9:
			if k[pos - 1] < sub:
				k[pos - 1] = sub
		else:
			continue
	print(sum(k))


n = int(input())
for i in range(n):
	d = {0: 0}
	s = 0
	x = int(input())
	for i in range(x):
		(p, q) = map(int, input().split())
		if p >= 1 and p <= 8:
			if p not in d:
				d[p] = q
			elif d[p] < q:
				d[p] = q
	for i in d.values():
		s += i
	print(s)


for _ in range(int(input())):
	n = int(input())
	maxs = dict()
	for i in range(1, n + 1):
		(p, s) = map(int, input().split())
		if p in maxs and p <= 8:
			maxs[p] = max(maxs[p], s)
		elif p <= 8:
			maxs[p] = s
	else:
		print(sum([v for (k, v) in maxs.items()]))


t = int(input())
for i in range(t):
	scores = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	n = int(input())
	for i in range(n):
		(l, k) = [int(x) for x in input().split()]
		scores[l - 1] = max(scores[l - 1], k)
	sums = 0
	for i in range(8):
		sums += scores[i]
	print(sums)


T = int(input())
for i in range(0, T):
	scoredict = {}
	Sub = int(input())
	for j in range(0, Sub):
		(Q, Pt) = map(int, input().split())
		if Q <= 8:
			if Q not in scoredict or scoredict[Q] < Pt:
				scoredict[Q] = Pt
	print(sum(scoredict.values()))


t = int(input())
for i in range(t):
	n = int(input())
	x = [0] * 8
	for j in range(n):
		(a, b) = map(int, input().split())
		if a <= 8:
			x[a - 1] = max(x[a - 1], b)
	print(sum(x))


for i in range(int(input())):
	scores = int(input())
	arr = dict()
	for i in range(scores):
		(X, Y) = map(int, input().split(' '))
		if X < 9:
			if str(X) not in arr:
				arr[str(X)] = Y
			elif arr[str(X)] < Y:
				arr[str(X)] = Y
		elif str(X) not in arr.items():
			arr[str(X)] = 0
		else:
			continue
	result = 0
	for (k, v) in arr.items():
		result += v
	print(result)


for i in range(int(input())):
	n = int(input())
	p = [0] * 8
	for i in range(n):
		(x, y) = map(int, input().split())
		if x <= 8:
			if p[x - 1] < y:
				p[x - 1] = y
	ans = 0
	for i in range(8):
		ans += p[i]
	print(ans)


t = int(input())
for i in range(t):
	n = int(input())
	a = [0] * 8
	for i in range(n):
		(x, y) = list(map(int, input().split()))
		if x <= 8:
			if a[x - 1] < y:
				a[x - 1] = y
	sum = 0
	for j in a:
		sum += j
	print(sum)


t = int(input())
for i in range(t):
	n = int(input())
	l1 = [0] * 12
	for i in range(n):
		(k, l) = map(int, input().split())
		if l1[k] < l and k < 9:
			l1[k] = l
	s = 0
	for i in l1:
		s = s + i
	print(s)


T = int(input())
for i in range(T):
	d = {}
	n = int(input())
	for j in range(n):
		(x, y) = map(int, input().split())
		if x not in d and x < 9:
			d[x] = y
		elif x in d and d[x] < y:
			d[x] = y
	print(sum(d.values()))


t = int(input())
for i in range(t):
	n = int(input())
	l = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	for i in range(n):
		(p, s) = map(int, input().split())
		if s > l[p]:
			l[p] = s
	print(sum(l[0:9]))


t = int(input())
for z in range(t):
	n = int(input())
	d = {}
	for i in range(n):
		(k, v) = [int(x) for x in input().split()]
		if k < 9:
			if k not in d:
				d[k] = v
			elif v > d[k]:
				d[k] = v
	if not d:
		print('0')
	else:
		x = d.values()
		x = list(x)
		print(sum(x))


t = int(input())
for i in range(t):
	n = int(input())
	a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
	b = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
	for j in range(n):
		(p, q) = map(int, input().split())
		for k in range(len(b)):
			if k == p - 1:
				if q > b[k]:
					b[k] = q
	b[8] = b[9] = b[10] = 0
	print(sum(b))


for i in range(int(input())):
	n = int(input())
	d = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0}
	for i in range(n):
		(k, v) = map(int, input().split())
		if k < 9 and d[k] < v:
			d[k] = v
	l = d.values()
	print(sum(l))


for _ in range(int(input())):
	n = int(input())
	d = {}
	for i in range(n):
		(a, b) = map(int, input().split())
		if a <= 8:
			try:
				if d[a] < b:
					d[a] = b
			except:
				d[a] = b
	s = sum(d.values())
	print(s)


for _ in range(int(input())):
	n = int(input())
	s = [0] * 8
	for i in range(n):
		(p, m) = list(map(int, input().split()))
		if p < 9 and s[p - 1] < m:
			s[p - 1] = m
	print(sum(s))


t = int(input())
for i in range(t):
	n = int(input())
	l = [0] * 12
	for j in range(0, n):
		(x, y) = map(int, input().split())
		if y > l[x]:
			l[x] = y
	sum = 0
	for k in range(1, 9):
		sum = sum + l[k]
	print(sum)


for _ in range(int(input())):
	d = {}
	s = int(input())
	for j in range(1, 12):
		d[j] = 0
	for i in range(s):
		(k, n) = map(int, input().split())
		if d[k] < n:
			d[k] = n
		else:
			continue
	result = 0
	for i in range(1, 9):
		result += d[i]
	print(result)


t = int(input())
for i in range(t):
	n = int(input())
	d = {}
	for j in range(n):
		(q, s) = list(map(int, input().split()))
		if q <= 8:
			if q in d:
				d[q] = max(d[q], s)
			else:
				d[q] = s
	sum = 0
	for key in d:
		sum = sum + d[key]
	print(sum)


t = int(input())
for i in range(t):
	ls = []
	n = int(input())
	sumv = 0
	for j in range(8):
		ls.append(0)
	for k in range(n):
		(x, y) = map(int, input().split())
		if x - 1 < 8:
			if ls[x - 1] < y:
				ls[x - 1] = y
	for l in range(8):
		sumv += ls[l]
	print(sumv)


for _ in range(int(input())):
	n = int(input())
	scores = [0] * 8
	for _ in range(n):
		(p, s) = map(int, input().split())
		if p <= 8:
			scores[p - 1] = max(scores[p - 1], s)
	print(sum(scores))


for i in range(int(input())):
	result = {}
	for j in range(1, 12):
		result[j] = 0
	submissions = int(input())
	for j in range(submissions):
		(problem, score) = map(int, input().split(' '))
		if result[problem] < score:
			result[problem] = score
		else:
			continue
	total_marks = 0
	for j in range(1, 9):
		total_marks += result[j]
	print(total_marks)


for i in range(int(input())):
	n = int(input())
	l = []
	sum = 0
	for i in range(n):
		(pi, ai) = map(int, input().split())
		l.append([ai, pi])
	l.sort()
	d = {i[1]: i[0] for i in l}
	for i in d.keys():
		if i <= 8:
			sum = sum + d[i]
	print(sum)


for _ in range(int(input())):
	n = int(input())
	a = [0, 0, 0, 0, 0, 0, 0, 0]
	for i in range(n):
		(n, m) = list(map(int, input().split()))
		if n == 9 or n == 10 or n == 11:
			continue
		elif a[n - 1] < m:
			a[n - 1] = m
	print(sum(a))


n = int(input())
for i in range(n):
	m = int(input())
	l = [0] * 8
	for i in range(m):
		(a, b) = map(int, input().split())
		if a > 0 and a <= 8:
			if l[a - 1] < b:
				l[a - 1] = b
	print(sum(l))


for i in range(int(input())):
	l = [0 for i in range(11)]
	for i in range(int(input())):
		(p, s) = map(int, input().split())
		l[p - 1] = max(l[p - 1], s)
	print(sum(l[:8]))