for count in range(int(input())):
n,m=map(int,input().split())
a,b=[0 for i in range(24)],[0 for i in range(24)]
for hh_count in range(n):
hh_s,hh_e=map(int,input().split())
#a[j]=[1] for j in range(hh_s,hh_e+1)
a[hh_s:hh_e+1]=[1]*(hh_e-hh_s+1)
for dd_count in range(m):
dd_s,dd_e=map(int,input().split())
#b[i]=[1] for i in range(dd_s,dd_e+1)
b[dd_s:dd_e+1]=[1]*(dd_e-dd_s+1)
total = 0
for i in range(24):
total=total+b[i]*a[i]
print(total)