def data(n):
a=[0 for i in range(24)]
for i in range(n):
x,y=map(int,input().split())
for j in range(x,y+1):
a[j]=1
return a
t=int(input())
for k in range(t):
a,b=map(int,input().split())
m,n=data(a),data(b)
sum=0
for i in range(24):
if m[i]==1 and n[i]==1:
sum+=1
print(sum)