def rddata(n):
a = [0 for i in range(24)]
for i in range(n):
s,e = list(map(int,input().split()))
for j in range(s,e+1):
a[j] = 1
return a;
ncase = int(input())
for t in range(ncase):
n,m = list(map(int,input().split()))
a,b=rddata(n),rddata(m)
sum=0
for i in range(24):
if a[i] == 1 and b[i] == 1:
sum += 1
print(sum)