// Arup Guha
// 3/12/2014
// Solution to 2014 FHSPS Playoff Problem: The Great Gatsby(gatsby)

import java.util.*;

public class gatsby {

	public static void main(String[] args) {

		Scanner stdin = new Scanner(System.in);
		int numCases = stdin.nextInt();

		// Go through each case.
		for (int loop=1; loop<=numCases; loop++) {

			// Read in Daisy's information.
			int daisyLoc = stdin.nextInt();
			pair[] daisyInfo = new pair[daisyLoc];
			for (int i=0; i<daisyLoc; i++) {
				int place = stdin.nextInt();
				int duration = stdin.nextInt();
				daisyInfo[i] = new pair(place, duration);
			}

			// Create her schedule.
			schedule daisy = new schedule(daisyInfo);

			// Read in Tom's information.
			int tomLoc = stdin.nextInt();
			pair[] tomInfo = new pair[tomLoc];
			for (int i=0; i<tomLoc; i++) {
				int place = stdin.nextInt();
				int duration = stdin.nextInt();
				tomInfo[i] = new pair(place, duration);
			}

			// Create his schedule.
			schedule tom = new schedule(tomInfo);

			// Just call the appropriate method!
			System.out.println(daisy.apart(tom));
		}
	}
}

// Simple class for OOP design.
class pair {

	public int loc;
	public int min;

	public pair(int place, int duration) {
		loc = place;
		min = duration;
	}
}

// Stores information for one person's daily schedule.
class schedule {

	public final static int MIN_PER_DAY = 1440;

	private int[] locList;

	// The key to make this easy is to store a person's location at
	// each minute of the day.
	public schedule(pair[] list) {

		locList = new int[MIN_PER_DAY];

		int curTime = 0;

		// Go through each event.
		for (int i=0; i<list.length; i++) {

			// Copy in this location for each minute we're there.
			for (int j=curTime; j<curTime+list[i].min; j++)
				locList[j] = list[i].loc;

			// Update current time.
			curTime = curTime+list[i].min;
		}
	}

	// Returns the number of minutes this and other are apart.
	public int apart(schedule other) {

		int cnt = 0;

		// Just go through each minute and add it if this and other are in different places.
		for (int i=0; i<MIN_PER_DAY; i++)
			if (this.locList[i] != other.locList[i])
				cnt++;
		return cnt;
	}
}