In the world of DragonBool there are fierce warriors called Soints. Also there are even fiercer warriors called Sofloats – the mortal enemies of Soints.

The power of each warrior is determined by the amount of chakra he possesses which is some positive integer. Warriors with zero level of chakra are dead warriors :) When the fight between Soint with power CI and Sofloat with power CF occurs the warrior with lower power will die and the winner will lose the amount of chakra that his enemy have possessed before the fight. So three cases are possible:

- CI > CF. Then Sofloat will die while the new power of Soint will be CI – CF.
- CI < CF. Then Soint will die while the new power of Sofloat will be CF – CI.
- CI = CF. In this special case both warriors die.

Each warrior (Soint or Sofloat) has his level of skills which is denoted by some positive integer. The fight between two warriors can occur only when these warriors are Soint and Sofloat of the same level. In particual, friendly fights are not allowed, i.e., a Soint cannot fight with another Soint and the same holds for Sofloats.

Lets follow the following convention to denote the warriors. A Soint of level L and power C will be denoted as (I, C, L), while Sofloat of level L and power C will be denoted as (F, C, L). Consider some examples. If A = (I, 50, 1) fights with B = (F, 20, 1), B dies and A becomes (I, 30, 1). On the other hand, (I, 50, 1) cannot fight with (F, 20, 2) as they have different levels.

There is a battle between Soints and Sofloats. There are N Soints and M Sofloats in all. The battle will consist of series of fights. As was mentioned above in each fight one Soint and one Sofloat of the same level take part and after the fight the warrior with lower power will die (or both will die if they have the same power). The battle proceeds as long as there exists at least one pair of warriors who can fight. The distribution of warriors by levels satisfies the following condition: for every Soint of level L there exists at least one Sofloat of the same level L and vice-versa. So if for some level L we have at least one warrior of this level then there is at least one Soint of level L and at least one Sofloat of level L.

There is a powerful wizard, whose name is SoChef, on the side of Soints. He can increase the amount of chakra of each Soint by any number. SoChef wants the army of Soints to win this battle. But increasing amount of chakra of any Soint by one costs him a lot of his magic power. Hence he wants to minimize the total amount of additional chakra he should give to Soints in order for them to win. Note, however, that the win here means that all Sofloats should be dead irregardless of whether any Soint is alive. Also note that the battle can proceed by different scenarios and the SoChef need to distribute additional chakra among the Soints in such a way that they will win for any possible battle scenario. Help SoChef and find the minimal amount of additional chakra he should give to Soints in order for them to win.

-----Input-----

The first line of the input contains an integer T, the number of test cases. T test cases follow. The first line of each test case contains two space separated integers N and M. Here N is the number of Soints  participating in the battle and M is the number of Sofloats  for the same. Each of the next N lines contains two space separated integers  Ci and  Li, the amount of chakra and level of i-th Soint correspondingly. The next M lines describe power and level of Sofloats participating in the battle in the same format.

-----Output-----

For each test case output a single integer on a single line, the minimum amount of chakra SoChef should give to Soints in order for them to win the battle.

-----Constraints-----
Each integer in the input file is positive and does not exceed 100. That is1 ≤ T ≤ 100
1 ≤ N ≤ 100
1 ≤ M ≤ 100
1 ≤ Ci ≤ 100
1 ≤ Li ≤ 100

For every Soint of level L there exists at least one Sofloat of the same level L and vice-versa.
It is guaranteed that each official test file will satisfy all these constraints. You DON'T need to verify them in your program.

-----Example-----
Input:
2
2 3
10 1
20 2
5 2
5 2
18 1
5 5
73 87
69 13
36 36
77 46
43 93
49 46
74 93
78 87
99 13
59 36

Output:
8
89

-----Explanation-----
Case 1.
The warriors are I1 = (I, 10, 1), I2 = (I, 20, 2), F1 = (F, 5, 2), F2 = (F, 5, 2), F3 = (F, 18, 1). Without the SoChef help the battle can proceed as follows.

- I2 fights with F1, F1 dies, I2 becomes (I, 15, 2).
- I2 fights with F2, F2 dies, I2 becomes (I, 10, 2).
- I1 fights with F3, I1 dies, F3 becomes (F, 8, 1).

So if SoChef will give 8 additional units of chakra to I1 the Soints will win the battle and even one Soint (I2) will left alive. Hence the answer is 8.

Difficulty: interview

Starter code:

Solutions:
t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


chakra = 0
t = eval(input())
for a in range (0, t) :
    chakra = 0
    s = input()
    l=s.split()
    n = int (l[0])
    m = int (l[1])
    lnew = []
    soint = []
    sofloat = []
    for i in range (0, n):
        s = input()
        l=s.split()
        soint += [[int (l[0]), int (l[1])]]
    for i in range (0, m):
        s = input()
        l=s.split()
        sofloat += [[int (l[0]), int (l[1])]]
    for i in range (0, n) :
        sumi = 0
        sumf = 0
        if not(soint[i][1] in lnew) :
            lnew+=[soint[i][1]]
            for j in range (i, n) :
                if soint[i][1] == soint[j][1] :
                    sumi+=soint[j][0]
            for k in range (0, m) :
                if soint[i][1] == sofloat[k][1] :
                    sumf+=sofloat[k][0]
            if sumi <= sumf :
                chakra += sumf - sumi
    print(chakra)



def main():
  t=int(input())
  while t :
    t=t-1
    n,m=input().split()
    x=100*[0]
    for i in range(int(n)):
      y=input().split()
      c=int(y[0])
      l=int(y[1])
      x[l-1]=x[l-1]+c
    z=100*[0]
    for i in range(int(m)):
      y=input().split()
      c=int(y[0])
      l=int(y[1])
      z[l-1]=z[l-1]+c
    s=0
    for i in range(100):
      if z[i]>x[i] :
        s=s+(z[i]-x[i])
    print(s)
    

def __starting_point():
  main()
__starting_point()

chakra = 0
t = eval(input())
for a in range (0, t) :
    chakra = 0
    s = input()
    l=s.split()
    n = int (l[0])
    m = int (l[1])
    lnew = []
    soint = []
    sofloat = []
    for i in range (0, n):
        s = input()
        l=s.split()
        soint += [[int (l[0]), int (l[1])]]
    for i in range (0, m):
        s = input()
        l=s.split()
        sofloat += [[int (l[0]), int (l[1])]]
    for i in range (0, n) :
        sumi = 0
        sumf = 0
        if not(soint[i][1] in lnew) :
            lnew+=[soint[i][1]]
            for j in range (i, n) :
                if soint[i][1] == soint[j][1] :
                    sumi+=soint[j][0]
            for k in range (0, m) :
                if soint[i][1] == sofloat[k][1] :
                    sumf+=sofloat[k][0]
            if sumi <= sumf :
                chakra += sumf - sumi
    print(chakra)


t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


t=eval(input())
for i in range(0,t):
    x=input()
    nm=x.split(' ')
    nm[0]=int(nm[0])
    nm[1]=int(nm[1])
    csoint=[]
    lsoint=[]
    csofloat=[]
    lsofloat=[]
    for j in range(0,nm[0]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csoint.append(b[0])
        lsoint.append(b[1])
    for k in range(0,nm[1]):
        a=input()
        b=a.split(' ')
        b[0]=int(b[0])
        b[1]=int(b[1])
        csofloat.append(b[0])
        lsofloat.append(b[1])
    chakra=0

##    for j in range(0,nm[0]):
##        if csofloat==[0]*nm[1]:
##                print chakra
##                break
##        for k in range(0,nm[1]):
##            if csofloat==[0]*nm[1]:
##                print chakra
##                break
##            if lsoint[j]==lsofloat[k]:
##                if csoint[j]>csofloat[k]:
##                    csoint[j]-=csofloat[k]
##                    csofloat[k]=0
##                    lsofloat[k]=0
##                elif csofloat[k]>csoint[j]:
##                    if lsoint.count(lsoint[j])==1==lsofloat.count(lsofloat[k]):
##                        chakra+=csofloat[k]-csoint[j]
##                        csoint[j]=csofloat[k]
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                    elif lsoint.count(lsoint[j])==1 and lsofloat.count(lsofloat[k])>lsoint.count(lsoint[j]):
##                        csoint[j]=csofloat[k]+1
##                        chakra+=csofloat[k]-csoint[j]+1
##                        csoint[j]=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
##                    else:
##                        csofloat[k]-=csoint[j]
##                        csoint[j]=0
##                        lsoint[j]=0
##                        break
##                else:
##                    if lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])>=lsofloat.count(lsofloat[k]):
##                        csoint[j]=0
##                        csofloat[k]=0
##                        lsoint[j]=0
##                        lsofloat[k]=0
##                        break
##                    elif lsoint.count(lsoint[j])==1 and lsoint.count(lsoint[j])<lsofloat.count(lsofloat[k]):
##                        csoint[j]=1
##                        chakra+=1
##                        csofloat[k]=0
##                        lsofloat[k]=0
    l=min(lsoint)-1
    ci=[]
    for a in range(l+1,max(lsoint)+1):
        c=0
        l+=1
        if l not in lsoint:
            continue
        for j in range(0,nm[0]):
           if lsoint[j]==l:
               c+=csoint[j]
        ci.append(c)
    
    l=min(lsofloat)-1
    cf=[]
    for a in range(l+1,max(lsofloat)+1):
        c=0
        l+=1
        if l not in lsofloat:
            continue
        for j in range(0,nm[1]):
            if lsofloat[j]==l:
                c+=csofloat[j]
        cf.append(c)

    for i in range(0,len(ci)):
        if ci[i]<cf[i]:
            chakra+=cf[i]-ci[i]
    print(chakra)


# fierce battles - codechef oct 2012 - drgnbool.py

t = int(input())

for testCase in range(t):
    n,m = list(map(int, input().split()))
    chakra = [0]*101
    for i in range(n):
        c,l = list(map(int, input().split()))
        chakra[l] += c
    for i in range(m):
        p,l = list(map(int, input().split()))
        chakra[l] -= p
    boost = 0
    for c in chakra:
        boost += max(0, -c)
    print(boost)
    


T = int(input())
chef = []
while (T>0):
    T-=1
    NM = input().split()
    N = int(NM[0])
    M = int(NM[1])

    soints = dict()
    sofloats = dict()

    for i in range(0,N):
        CL = input().split()
        C = int(CL[0])
        L = int(CL[1])
        if L in soints:
            soints[L]+=C
        else:
            soints[L] = C

    for i in range(0,M):
        CL = input().split()
        C = int(CL[0])
        L = int(CL[1])
        if L in sofloats:
            sofloats[L]+=C
        else:
            sofloats[L] = C

    chef_val = 0
    for i in soints:
        if sofloats[i] - soints[i] > 0:
            chef_val += sofloats[i] - soints[i]
                    
    chef.append(chef_val)
for i in chef:
    print(i)


'''
Created on Oct 9, 2012

@author: Nitish
'''

def solve():
    levels = {}
    power = 0
    N, M = list(map(int, input().split()))
    for i in range(N):
        C, L = list(map(int, input().split()))
        if L in levels:
            levels[L] += C 
        else:
            levels[L] = C
    for f in range(M):
        C, L = list(map(int, input().split()))
        levels[L] -= C
    
    for key, value in list(levels.items()):
        if value < 0:
            power += value
    
    print(abs(power))    

def __starting_point():
    T = int(input())
    while T > 0:
        solve()
        T -= 1
__starting_point()

from functools import reduce
def add(x, y):
    a = n_d[y] - m_d[y]
    return a+x if a>0 else x

a = int(input())
for u in range(a):
    m, n = list(map(int, input().split()))
    m_d = {}
    n_d = {}
    for v in range(m):
        k, j = list(map(int, input().split()))
        if(j in m_d):
            m_d[j] += k
        else:m_d[j] = k
    for w in range(n):
        k, j = list(map(int, input().split()))
        if(j in n_d):
            n_d[j] += k
        else:n_d[j] = k
    print(reduce(add, iter(m_d.keys()), 0))



from sys import stdin
t=(stdin.readline())
levels=[0]*101
for i in range(int(t)):
   sochef=0
   warriors = stdin.readline()
   n,m = warriors.split()
   for i in range(int(n)):
      x=stdin.readline()
      c,l=x.split()
      levels[int(l)]+=int(c)
   for i in range(int(m)):
      x=stdin.readline()
      c,l=x.split()
      levels[int(l)]-=int(c)
   for i in range(101):
      if levels[i]<0:
         sochef+=levels[i]
      levels[i] = 0
   print(abs(sochef))


from sys import stdin
t=(stdin.readline())
levels=[0]*101
for i in range(int(t)):
   sochef=0
   warriors = stdin.readline()
   n,m = warriors.split()
   for i in range(int(n)):
      x=stdin.readline()
      c,l=x.split()
      levels[int(l)]+=int(c)
   for i in range(int(m)):
      x=stdin.readline()
      c,l=x.split()
      levels[int(l)]-=int(c)
   for i in range(101):
      if levels[i]<0:
         sochef+=levels[i]
      levels[i] = 0
   print(abs(sochef))


t=int(input())
levels=[0]*101
for i in range(t):
   sochef=0
   warriors = input()
   n,m = warriors.split()
   for i in range(int(n)):
      x=input()
      c,l=x.split()
      levels[int(l)]+=int(c)
   for i in range(int(m)):
      x=input()
      c,l=x.split()
      levels[int(l)]-=int(c)
   for i in range(101):
      if levels[i]<0:
         sochef+=levels[i]
      levels[i] = 0
   print(abs(sochef))


T = int(input())

for t in range(0, T):
    N, M = input().split()

    I = []
    F = []
    
    for n in range(0, int(N)):
        C, L = input().split()
        I.append((int(C), int(L)))
        
    for n in range(0, int(M)):
        C, L = input().split()
        F.append((int(C), int(L)))

    chakra = 0

    for l in set([i[1] for i in I]):
        Cil = [i[0] for i in I if i[1] == l]
        Cfl = [f[0] for f in F if f[1] == l]
        temp = (sum(Cfl) - sum(Cil)) 
        if temp > 0:
            chakra += temp

    print(chakra)


from functools import reduce
def add(x, y):
    a = n_d[y] - m_d[y]
    return a+x if a>0 else x

a = int(input())
for u in range(a):
    m, n = [int(x) for x in input().split(' ')]
    m_d = {}
    n_d = {}
    for v in range(m):
        k, j = [int(x) for x in input().split(' ')]
        if(j in m_d):
            m_d[j] += k
        else:m_d[j] = k
    for w in range(n):
        k, j = [int(x) for x in input().split(' ')]
        if(j in n_d):
            n_d[j] += k
        else:n_d[j] = k
    print(reduce(add, iter(m_d.keys()), 0))



from functools import reduce
def add(x, y):
    a = n_d[y] - m_d[y]
    return a+x if a>0 else x

a = int(input())
while(a):
    m, n = [int(x) for x in input().split(' ')]
    m_d = {}
    n_d = {}
    while(m):
        k, j = [int(x) for x in input().split(' ')]
        if(j in m_d):
            m_d[j] += k
        else:m_d[j] = k
        m-=1
    while(n):
        k, j = [int(x) for x in input().split(' ')]
        if(j in n_d):
            n_d[j] += k
        else:n_d[j] = k
        n-=1
    a-=1
    print(reduce(add, iter(m_d.keys()), 0))



chakra = 0
t = eval(input())
for a in range (0, t) :
    chakra = 0
    s = input()
    l=s.split()
    n = int (l[0])
    m = int (l[1])
    lnew = []
    soint = []
    sofloat = []
    for i in range (0, n):
        s = input()
        l=s.split()
        soint += [[int (l[0]), int (l[1])]]
    for i in range (0, m):
        s = input()
        l=s.split()
        sofloat += [[int (l[0]), int (l[1])]]
    for i in range (0, n) :
        sumi = 0
        sumf = 0
        if not(soint[i][1] in lnew) :
            lnew+=[soint[i][1]]
            for j in range (i, n) :
                if soint[i][1] == soint[j][1] :
                    sumi+=soint[j][0]
            for k in range (0, m) :
                if soint[i][1] == sofloat[k][1] :
                    sumf+=sofloat[k][0]
            if sumi <= sumf :
                chakra += sumf - sumi
    print(chakra)

# ******************************************************************************
# Problem: Fierce Battles
# Link: http://www.codechef.com/OCT12/problems/DRGNBOOL
# Author: Deepak Antony - "deebee" "drunkbeast" "dAbeAst"
# Solution: simple; sum up the total for each level and subtract from the 
# sofloats total capacity for that level; in the end sum up all levels with
# positive diff's
# ******************************************************************************

# Uncomment this line if the judge supports psyco
# import psyco
# psyco.full()

import time
import sys

def timeElapsed(fn):
    """ A time elapsed decorator """
    def wrapper(*args, **kwargs):
        start = time.time()
        res = fn(*args, **kwargs)
        sys.stderr.write("Time Elapsed: %f seconds\n"%(time.time()-start))
        return res
    return wrapper

def solveFierceBattles():
    T = int(input())
    for test in range(T):
        N, M = tuple( int(x) for x in input().split() )
        soints = {}
        for ints in range(N):
            chakra, level = tuple( int(x) for x in input().split() )
            if level not in soints:
                soints[level] = chakra
            else:
                soints[level] += chakra
        sofloats = {}
        for floats in range(M):
            chakra, level = tuple( int(x) for x in input().split() )
            if level not in sofloats:
                sofloats[level] = chakra
            else:
                sofloats[level] += chakra
        res = 0
        for level, chakra in soints.items():

            if chakra < sofloats[level]:
                res += sofloats[level]-chakra
        print(res)
        

@timeElapsed
def main():
    """ Main controller """
    # solve a problem here
    solveFierceBattles()

def __starting_point():
    main()

__starting_point()

seq = int(input())
while seq>0:
    level = [0]*101
    n,m = input().split()
    m = int(m)
    n = int(n)
    power =0
    while n>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] + c
        n = n -1;
    while m>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] - c
        m = m -1;
    for no in level:
        if no<0:
            power = power + (-no)
    print(power)	
    seq =seq - 1


seq = int(input())
while seq>0:
    level = [0]*101
    n,m = input().split()
    m = int(m)
    n = int(n)
    power =0
    while n>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] + c
        n = n -1;
    while m>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] - c
        m = m -1;
    for no in level:
        if no<0:
            power = power + (-no)
    print(power)	
    seq =seq - 1


seq = int(input())
while seq>0:
    level = [0]*101
    n,m = input().split()
    m = int(m)
    n = int(n)
    power =0
    while n>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] + c
        n = n -1;
    while m>0:
        c,l = input().split()
        c = int(c)
        l = int(l)
        level[l] = level[l] - c
        m = m -1;
    for no in level:
        if no<0:
            power = power + (-no)
    print(power)	
    seq =seq - 1


def go():
    n,m = list(map(int, input().split()))
    d = [0]*101
    for i in range(n):
        c,l = list(map(int, input().split()))
        d[l] += c
    for i in range(m):
        c,l = list(map(int, input().split()))
        d[l] -= c
    print(sum(-i for i in d if i<0))

for t in range(eval(input())): go()