for tcase in range(int(input())):
n,m=map(int,input().split())
a,b=set(),set()
for i in range(n):
s,e=map(int,input().split())
a=a|set(range(s,e+1))
for i in range(m):
s,e=map(int,input().split())
b=b|set(range(s,e+1))
print(len(a&b))