n=int(input())
i=0
while i<n:
j,k=0,0
hh=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
dd=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
a,b=map(int,input().split())
while j<a:
c,d=map(int,input().split())
for p in range(c,d+1):
hh[p]=1
j+=1
while k<b:
e,f=map(int,input().split())
for q in range(e,f+1):
dd[q]=1
k+=1
z=0
cnt=0
while z<24:
if hh[z]==1 and dd[z] == 1:
cnt=cnt+1
z=z+1
print(cnt)
i+=1