#include<iostream>
using namespace std;

int main(){
	while(1){
		bool dp[1000][1000] = {false};
		int n, m, h, r, ans = 0;
		cin >>n;
		if(n == 0) break;
		for(int i = 0; i < n; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		cin >>m;
		for(int i = 0; i < m; i++){
			cin >>h >>r;
			dp[h][r] = true;
		}
		int cnt = 1;
		for(int i = cnt; i < 1000; i++){
			for(int j = cnt; j < 1000; j++){
				if(dp[i][j] == true || dp[j][i] == true){
					ans++;
					break;
				}
			}
			cnt++;
		}
		cout <<ans <<endl;
	}
	return 0;
}