Read problems statements in Mandarin Chinese  and Russian. 

Suraj, the Chief Prankster is back in action now and this time he has stolen the valentine's day gift given by Ashi (the love of Chef) to the Chef and ran away with it to Byteland.
Byteland is a not a regular place like Chef's town. The safest way from Chef's town to Byteland is through the path of tasty dishes. The path is named so because there are magical tasty dishes which appear to the traveler that no one can resist eating. Also, Suraj has added a strong sleep potion to each of the dish on this path to stop anyone from following him.
Knowing the devilish nature of Suraj, Ashi is concerned about the Chef and has asked all of Chef's town people to help. The distance from Chef's town to Byteland through the the path of tasty dishes is X units. They have the location where the magic dishes are and how many people are required to eat it completely. Anyone who eats a dish would go to a long sleep and won't be able to continue. They have the information about the tribal clans that live along the the path of tasty dishes who can be of real help in this journey. 
The journey Chef and his friends can be described as follows: There is a total of B dishes on the path of tasty dishes. Each dish is located at some distance from Chef's town denoted by x_{i} for the i^{th} dish ( x_{i-1} <  x_{i}). To minimize the number of friends Chef has to leave behind, all of them have decided that exactly y_{i} of them will eat the i^{th} dish, which is the required number of people needed to finish it completely. Also, there are a total of C tribal chef clans, each with their own population and location on the path that Chef and his friends will meet on their way to Byteland. They know that for some clan (say i), they are located at a distance of p_{i} ( p_{i-1} <  p_{i}) from Chef's town with a population of r_{i}. And if a group of at least q_{i} men approaches them, they would be able to convince them to join their forces against Suraj.

Given the information about all this, help the Chef to find out the minimum size of the group (including him and his friends) he should start with to reach Byteland and get back Ashi's gift from Suraj.

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

The first line of the input contains an integer T denoting the number of test cases. Each test case contains three lines which are as follows:

First line of each test case contains X, the distance of Byteland from Chef's town.

Next line contains an integer B, the number of dishes on the path of tasty dishes. Then follows B pairs of space separated integers of the form x_{i} y_{i}, where x_{i} y_{i} are as defined above for the i^{th} dish.
Next line contains an integer C, followed C space separated triplets of integers p_{i} q_{i} r_{i} as defined above.

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

For each test case, print the minimum size of the group  (including Chef) that is needed to reach Byteland.

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

1 ≤ T ≤ 10
1 ≤ X ≤ 10^{9}
1 ≤ B ≤ 10000
Constraints on C

Subproblem 1 (25 points):  C = 0
Subproblem 2 (75 points): 1 ≤ C ≤ 10000

1 ≤ x_{i} < X, x_{i} < x_{i+1}
1 ≤ p_{i} < X, p_{i} < p_{i+1}
1 ≤ y_{i} ≤ 10^{14}
1 ≤ q_{i} ≤ 10^{14}
1 ≤ r_{i} ≤ 10^{14}
All the positions, of the tasty dishes and tribal clans are distinct.

----- Sample Input 1 ------ 
3

10

2 1 3 8 1

0

10

2 1 3 8 5

0

10

2 2 3 8 5

3 1 2 1 4 3 2 9 1 1
----- Sample Output 1 ------ 
5

9

6
----- explanation 1 ------ 
Example case 1. In the first case, there are no tribal clans, and two dishes, one which needs to be eaten by 3 chefs on their way and one to be eaten by 1 chef. Hence, we have to start with atleast 5 people in total to pass the path of tasty dishes.
Example case 2. Similar as Example Case 1.
Example case 3. In this case, if we start with 5 Chefs. At point 1, we have more than or equal to 2 chefs, hence the tribal clan of size 1 adds to the Chef's party and now they have size of 6. At position 2, three of them would be left behind eating a dish, leaving 3 of them to go ahead. At position 4, since the size is exactly 3, the tribal clan joins the chef's party making it of size 5. At position 8, all 5 of them will stop to eat the dish and none would go ahead. Similarly, if we start with 6, one of them would be able to pass position 8 and reach position 9, where it will also add one of the tribal clans to its party and reach Byteland.

Difficulty: HARD

Solutions:
import bisect
import sys
import math

def get_ints():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))

def get_string():
	return sys.stdin.readline().strip()

def get_int():
	return int(sys.stdin.readline().strip())

def get_list_strings():
	return list(map(str, sys.stdin.readline().strip().split()))

def solve():
	distance = get_int()
	arr1 = get_list()
	arr2 = get_list()
	store1 = []
	store2 = []
	r = 1
	b = arr1[0]
	c = arr2[0]
	for i in range(1, len(arr1), 2):
		store1.append([arr1[i], arr1[i + 1]])
		r += arr1[i + 1]
	for i in range(1, len(arr2), 3):
		store2.append([arr2[i], arr2[i + 1], arr2[i + 2]])
	l = 1
	ans = r
	while l <= r:
		m = (l + r) // 2
		mid = m
		i = 0
		j = 0
		flag = 0
		while (i < len(store1) and j < len(store2)) and flag == 0:
			if store1[i][0] > store2[j][0]:
				if m >= store2[j][1]:
					m += store2[j][2]
				j += 1
			elif store1[i][0] < store2[j][0]:
				m -= store1[i][1]
				if m <= 0:
					flag = 1
				i += 1
			else:
				if m >= store2[j][1]:
					m += store2[j][2]
				m -= store1[i][1]
				if m <= 0:
					flag = 1
				i += 1
				j += 1
		if flag == 1:
			l = mid + 1
			continue
		while i < len(store1) and flag == 0:
			m -= store1[i][1]
			if m <= 0:
				flag = 1
			i += 1
		if flag == 1:
			l = mid + 1
		else:
			ans = mid
			r = mid - 1
	print(ans)
T = get_int()
while T:
	solve()
	T -= 1


def fun(x, X, B, C, d, c):
	i = 0
	j = 0
	count = 0
	per = x
	while i < 2 * d:
		di = B[i]
		if j + 2 < c * 3:
			pj = C[j]
		else:
			pj = None
		if pj != None and pj == di:
			if per >= C[j + 1]:
				per += C[j + 2]
				j += 3
			per -= B[i + 1]
			if per <= 0:
				break
			i += 2
		elif pj != None and pj < di:
			if per >= C[j + 1]:
				per += C[j + 2]
			j += 3
		else:
			per -= B[i + 1]
			if per <= 0:
				break
			i += 2
	if per >= 1:
		return True
	else:
		return False

def binaryfun(d, c, B, C, i, x, X):
	if i <= x:
		m = (x + i) // 2
		if fun(m, X, B, C, d, c) == True:
			return binaryfun(d, c, B, C, i, m - 1, X)
		else:
			return binaryfun(d, c, B, C, m + 1, x, X)
	else:
		return max(i, x)
for _ in range(int(input())):
	X = int(input())
	B = [int(x) for x in input().split()]
	d = B.pop(0)
	C = [int(x) for x in input().split()]
	c = C.pop(0)
	x = 0
	for i in range(1, len(B), 2):
		x += B[i]
	x += 1
	if c == 0:
		print(x)
	else:
		print(binaryfun(d, c, B, C, 1, x, X))