Time limit
2s
Memory limit
128 MB
Two freight trains are on neighboring rails. Each car may or may not contain a container. Train A stays fixed, and only Train B can be moved forward. We want to move Train B so that as many occupied cars as possible line up with occupied cars of Train A.
Initially, the front edges of the first cars of the two trains face each other on the same line. If Train B is moved forward by k cars, car i of Train A and car j of Train B occupy the same position exactly when i + j - 1 = k. Find how many cars Train B must be moved forward to maximize the number of aligned occupied cars.

The first line contains N, the number of consecutive occupied intervals in Train A. Each of the next N lines contains Xi and Yi, separated by a space. This means that every car from car Xi through car Yi of Train A contains a container.
The next line contains M, the number of consecutive occupied intervals in Train B. Each of the next M lines contains Zi and Wi, separated by a space. This means that every car from car Zi through car Wi of Train B contains a container.
N and M are natural numbers from 1 to 1,000. Every car number is a natural number at most 10^9, and the start of each interval is not greater than its end.
Print the number of cars by which Train B should be moved forward so that the number of aligned occupied cars is as large as possible. If multiple shifts achieve the maximum, print the smallest such shift.