Chef has a binary tree. The binary tree consists of 1 or more nodes. Each node has a unique integer id. Each node has up to 2 children, which are identified by their ids, and each node is the child of at most 1 other node. A node X is considered to be an ancestor of node Y if node Y is a child of node X or if there is some node Z for which X is an ancestor of Z and Y is a child of Z. No node is an ancestor of itself.  A special node called the root node is an ancestor of all other nodes.
Chef has forgotten which node of his tree is the root, and wants you to help him to figure it out. Unfortunately, Chef's knowledge of the tree is incomplete. He does not remember the ids of the children of each node, but only remembers the sum of the ids of the children of each node.

------ Input ------ 

Input begins with an integer T, the number of test cases. Each test case begins with an integer N, the number of nodes in the tree. N lines follow with 2 integers each: the id of a node, and the sum of the ids of its children. The second number will be 0 if the node has no children.

------ Output ------ 

For each test case, output on a line a space separated list of all possible values for the id of the root node in increasing order. It is guaranteed that at least one such id exists for each test case.

------ Constraints ------ 

$1 ≤ T ≤ 50$
$1 ≤ N ≤ 30$
$All node ids are between 1 and 1000, inclusive$

------ Sample Input ------ 

2
1
4 0
6
1 5
2 0
3 0
4 0
5 5
6 5

------ Sample Output ------ 

4
6

------ Explanation ------ 

In the first sample test case, there is only one node, which is clearly the root. In the second test case, there are two non-isomorphic trees that satisfy the constraints, as seen in the following picture:
6           6
\         / \
5       1   4
/ \       \
1   4       5
/ \         / \
2   3       2   3

Difficulty: HARD
Tags: Mathematics

Starter code:

Solutions:
t = int(input())
for i in range(t):
	n = int(input())
	s = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		s += a - b
	print(s)


for _ in range(int(input())):
	id = child = 0
	for i in range(int(input())):
		(a, b) = map(int, input().split())
		id += a
		child += b
	print(id - child)


t = int(input())
for _ in range(t):
	n = int(input())
	id_sum = 0
	sum = 0
	for i in range(n):
		(idn, s) = map(int, input().split())
		id_sum += idn
		sum += s
	print(id_sum - sum)


t = int(input())
for tc in range(0, t):
	r = []
	sum_id = []
	n = int(input())
	for n in range(0, n):
		(i_d, value) = map(int, input().split(' '))
		sum_id.append(i_d)
		r.append(value)
	sum_ids = sum(sum_id)
	sum_r = sum(r)
	root = sum_ids - sum_r
	print(root)


for i in range(int(input())):
	n = int(input())
	l = []
	s = []
	for j in range(n):
		(x, y) = map(int, input().split())
		l.append(x)
		s.append(y)
	print(sum(l) - sum(s))


T = int(input())
for _ in range(T):
	N = int(input())
	root = 0
	for j in range(N):
		(ide, c) = map(int, input().split(' '))
		root += ide - c
	print(root)


for _ in range(eval(input())):
	n = eval(input())
	r = 0
	for i in range(n):
		(i, c) = map(int, input().split())
		r += i - c
	print(r)


t = int(input())
for _ in range(t):
	n = int(input())
	total = 0
	s = 0
	for _ in range(n):
		(val, id) = list(map(int, input().split()))
		total += val
		s += id
	print(total - s)


import sys, math
for _ in range(int(input())):
	n = int(input())
	(s, ss) = (0, 0)
	for i in range(n):
		(a, b) = [int(c) for c in input().split()]
		s += a
		ss += b
	print(s - ss)


for _ in range(int(input())):
	sum_data = 0
	for i in range(int(input())):
		data = list(map(int, input().split()))
		sum_data += data[0] - data[1]
	print(sum_data)


t = int(input())
for _ in range(t):
	n = int(input())
	ids = 0
	sumi = 0
	for __ in range(n):
		(id1, su) = map(int, input().split())
		ids = ids + id1
		sumi = sumi + su
	print(ids - sumi)


t = int(input())
for x in range(t):
	n = int(input())
	tree = []
	treeRoot = 0
	for i in range(n):
		tree.append(input().split())
		treeRoot += int(tree[i][0]) - int(tree[i][1])
	print(treeRoot)


for i in range(int(input())):
	ans = 0
	for j in range(int(input())):
		(a, b) = map(int, input().split())
		ans += a - b
	print(ans)


n = int(input())
for i in range(n):
	a = int(input())
	z = 0
	for j in range(a):
		(b, c) = map(int, input().split())
		z += b - c
	print(z)


t = int(input())
for i in range(t):
	n = int(input())
	l1 = []
	l2 = []
	for j in range(0, n):
		[a, b] = list(map(int, input().split()))
		l1.append(a)
		l2.append(b)
	print(sum(l1) - sum(l2))


def tree():
	t = int(input())
	while t:
		left_sum = 0
		right_sum = 0
		n = int(input())
		for i in range(n):
			(l_node, r_node) = [int(x) for x in input().split()]
			left_sum += l_node
			right_sum += r_node
		print(left_sum - right_sum)
		t -= 1
tree()


for _ in range(int(input())):
	n = int(input())
	mapp = {}
	for i in range(n):
		(node, child) = map(int, input().split())
		mapp[node] = child
	print(sum(mapp.keys()) - sum(mapp.values()))


for _ in range(int(input())):
	n = int(input())
	if n <= 30:
		x = 0
		y = 0
		for _ in range(n):
			(a, b) = map(int, input().split())
			x += a
			y += b
		print(x - y)
	else:
		print(0)
		exit()


t = int(input())
while t:
	l = r = 0
	for _ in range(int(input())):
		(a, b) = map(int, input().split())
		l += a
		r += b
	print(l - r)
	t -= 1


for _ in range(int(input())):
	n = int(input())
	x = 0
	y = 0
	for _ in range(n):
		(a, b) = map(int, input().split())
		x += a
		y += b
	print(x - y)


a = int(input())
for i in range(a):
	(s, s1) = (0, 0)
	b = int(input())
	for j in range(b):
		c = list(map(int, input().split()))
		s += c[0]
		s1 += c[1]
	print(abs(s1 - s))


t = int(input())
for i in range(0, t):
	run_sum = 0
	n = int(input())
	for x in range(0, n):
		(a, b) = map(int, input().split())
		run_sum += a - b
	print(run_sum)


t = int(input())
for i in range(t):
	(val, ids) = (0, 0)
	n = int(input())
	for j in range(n):
		(l_node, r_node) = map(int, input().split())
		val += l_node
		ids += r_node
	print(val - ids)


for _ in range(int(input())):
	n = int(input())
	root = 0
	for i in range(n):
		(id, c) = map(int, input().split())
		root += id - c
	print(root)


t = int(input())
for _ in range(t):
	n = int(input())
	s1 = 0
	s2 = 0
	for _ in range(n):
		(x, y) = map(int, input().split())
		s1 = s1 + x
		s2 = s2 + y
	print(s1 - s2)


for _ in range(int(input())):
	n = int(input())
	mapping = {}
	for i in range(n):
		(node, sum_of_child) = map(int, input().split())
		mapping[node] = sum_of_child
	print(sum(mapping.keys()) - sum(mapping.values()))


t = int(input())
while t > 0:
	t -= 1
	n = int(input())
	ans = 0
	for i in range(n):
		tmp = input().split(' ')
		ans += int(tmp[0]) - int(tmp[1])
	print(ans)


testcase = int(input())
for _ in range(testcase):
	num = int(input())
	l = []
	C_sum = 0
	for i in range(num):
		(Id, C) = map(int, input().split())
		l.append(Id)
		C_sum += C
	print(sum(l) - C_sum)


t = int(input())
while t > 0:
	n = int(input())
	root = 0
	while n > 0:
		(i, s) = map(int, input().split())
		root += i - s
		n -= 1
	print(root)
	t -= 1


getd = int(input())
for _ in range(getd):
	Ranga = int(input())
	root = 0
	for j in range(Ranga):
		(kadhu, manam) = map(int, input().split(' '))
		root += kadhu - manam
	print(root)


for x in range(int(input())):
	n = int(input())
	root = 0
	for _ in range(n):
		p = input().split()
		root += int(p[0]) - int(p[1])
	print(root)


for _ in range(int(input())):
	n = int(input())
	x = 0
	for i in range(n):
		(a, b) = tuple(map(int, input().split()))
		x += a - b
	print(x)


for _ in range(int(input())):
	sum = 0
	sum2 = 0
	for _ in range(int(input())):
		(nodes, child) = map(int, input().split())
		sum += nodes
		sum2 += child
	print(sum - sum2)


T = int(input())
for i in range(T):
	N = int(input())
	f_value = 0
	for j in range(N):
		(id, value) = input().split(' ')
		f_value += int(id) - int(value)
	print(f_value)


for i in range(int(input())):
	n = int(input())
	l = []
	sum1 = 0
	for i in range(n):
		(k, m) = map(int, input().split())
		l.append(k)
		sum1 += m
	print(sum(l) - sum1)


for j in range(int(input())):
	sum1 = 0
	sum2 = 0
	for i in range(int(input())):
		k = input().split()
		sum1 += int(k[0])
		sum2 += int(k[1])
	print(sum1 - sum2)


t = int(input())
for i in range(t):
	n = int(input())
	ar = []
	br = []
	for j in range(0, n):
		[a, b] = list(map(int, input().split()))
		ar.append(a)
		br.append(b)
	print(sum(ar) - sum(br))


for i in range(int(input())):
	root = 0
	for j in range(int(input())):
		(node, total) = map(int, input().split())
		root += node - total
	print(root)


t = int(input())
for i in range(t):
	N = int(input())
	ids = []
	sum1 = []
	for i in range(N):
		(id1, s) = map(int, input().split())
		ids.append(id1)
		sum1.append(s)
	root = sum(ids) - sum(sum1)
	print(root)


for _ in range(int(input())):
	k = 0
	for _ in range(int(input())):
		(x, y) = map(int, input().split())
		k = k + x - y
	print(k)


for _ in range(int(input())):
	n = int(input())
	s = 0
	for _ in range(n):
		(x, y) = list(map(int, input().split()))
		s += x - y
	print(s)


x = int(input())
res_list = []
for each in range(x):
	y = int(input())
	summ = 0
	for i in range(y):
		data = list(map(int, input().split()))
		summ += data[0] - data[1]
	res_list.append(summ)
for each in res_list:
	print(each)


for t in range(0, int(input())):
	n = int(input())
	id = 0
	sid = 0
	for i in range(0, n):
		(a, b) = map(int, input().split())
		id += a
		sid += b
	print(id - sid)


for i in range(int(input())):
	n = int(input())
	ids = []
	sums = []
	for j in range(n):
		(node_id, children_sum) = map(int, input().split())
		ids.append(node_id)
		sums.append(children_sum)
	print(sum(ids) - sum(sums))


n = int(input())
for _ in range(n):
	(sum_ar, sum_id) = (0, 0)
	for __ in range(int(input())):
		(x, y) = map(int, input().split())
		sum_ar += x
		sum_id += y
	print(sum_ar - sum_id)


for _ in range(int(input())):
	n = int(input())
	root = 0
	for i in range(0, n):
		(a, b) = map(int, input().split())
		root = root + (a - b)
	print(root)


def solve():
	n = int(input())
	l = r = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		l += a
		r += b
	print(l - r)
t = int(input())
while t:
	solve()
	t -= 1


for _ in range(int(input())):
	ids = []
	sums = []
	for _ in range(int(input())):
		(x, y) = map(int, input().split())
		ids.append(x)
		sums.append(y)
	print(sum(ids) - sum(sums))


def arrSum(arr):
	add = 0
	for i in range(0, len(arr)):
		add += arr[i]
	return add
for __ in range(0, int(input())):
	Id = []
	Sum = []
	for i in range(0, int(input())):
		(a, b) = input().split()
		a = int(a)
		b = int(b)
		Id.append(a)
		Sum.append(b)
	root = arrSum(Id) - arrSum(Sum)
	print(root)


t = int(input())
for _ in range(t):
	n = int(input())
	root = 0
	for j in range(n):
		(ide, c) = map(int, input().split(' '))
		root += ide - c
	print(root)


def solve():
	n = int(input())
	ans = 0
	for i in range(n):
		(id, sm) = map(int, input().split())
		ans += id - sm
	return ans
t = int(input())
while t != 0:
	res = solve()
	print(res)
	t -= 1


for _ in range(int(input())):
	root = 0
	for i in range(int(input())):
		(n, m) = map(int, input().split())
		root += n - m
	print(root)


t = int(input())
for _ in range(t):
	n = int(input())
	total = 0
	sums = 0
	for i in range(n):
		(ids, su) = map(int, input().split())
		total = total + ids
		sums = sums + su
	print(total - sums)


t = int(input())
for i in range(t):
	n = int(input())
	root = 0
	for j in range(n):
		a = list(map(int, input().split()))
		(id, kid) = a
		root += id - kid
	print(root)


for _ in range(int(input())):
	n = int(input())
	si = 0
	sl = 0
	for i in range(n):
		(a, b) = map(int, input().split())
		si += a
		sl += b
	print(abs(si - sl))


T = int(input())
lista = []
if T >= 1 and T <= 50:
	for i in range(T):
		N = int(input())
		r = 0
		if N >= 1 and N <= 30:
			for j in range(N):
				(n, h) = map(int, input().split())
				r += n - h
			lista.append(r)
	for e in lista:
		print(e)


t = int(input())
while t:
	n = int(input())
	sum1 = sum2 = 0
	for i in range(0, n):
		(x, y) = map(int, input().split())
		sum1 += x
		sum2 += y
	print(sum1 - sum2)
	t -= 1


a = int(input())
for i in range(a):
	q = int(input())
	z = 0
	for j in range(q):
		(b, c) = input().split()
		z += int(b) - int(c)
	print(z)


try:
	for _ in range(int(input())):
		ans = 0
		n = int(input())
		for i in range(n):
			(id, s) = map(int, input().split(' '))
			ans += id - s
		print(ans)
except:
	pass


for _ in range(int(input())):
	ans = 0
	n = int(input())
	for _ in range(n):
		(i, j) = map(int, input().split())
		ans += i - j
	print(ans)