hht=[]
dt=[]
s=0
for i in range(int(input())):
n,m=map(int,input().split())
for j in range(n):
a,b=map(int,input().split())
for hh in range (a,b+1):
if hh not in hht:
hht.append(hh)
for k in range(m):
c,d=map(int,input().split())
for e in range(c,d+1):
if e not in dt:
dt.append(e)
for t in hht:
if t in dt:
s+=1
print(s)
s=0
hht=[]
dt=[]