Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"sort"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		hr := make([]entry, 0)
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf := strings.Split(scanner.Text(), " ")
			first, _ := strconv.Atoi(buf[0])
			second, _ := strconv.Atoi(buf[1])
			mt := entry{first, second}
			hr = append(hr, mt)
		}
		sort.Sort(ByFirst(hr))
		fmt.Println(LIS(hr))
	}
}

type entry struct {
	first, second int
}

type ByFirst []entry

func(f ByFirst) Len() int {
	return len(f)
}

func (f ByFirst) Swap(i, j int) {
	f[i], f[j] = f[j], f[i]
}

func (f ByFirst) Less(i, j int) bool {
	return f[i].first < f[j].first
}

func LIS(hr []entry) int {
	n := len(hr)
	lis := make([]int, n)
	lis[n-1] = 1
	for i := n-2; i > -1; i-- {
		m := 0
		for j := i+1; j < n; j++ {
			if hr[i].first < hr[j].first && hr[i].second < hr[j].second && lis[j] > m {
				m = lis[j]
			}
		}
		lis[i] = m+1
	}
	return lis[0]
}

Python: while True:
  n = int(input())
  if n == 0:
    break
  hr_lst = []
  for _ in range(n):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  m = int(input())
  for _ in range(m):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  hr_lst.sort(reverse=True)

  r_lst = [[] for _ in range(1001)]
  for h, r in hr_lst:
    r_lst[h].append(r)
  r_lst = [lst for lst in r_lst if lst != []]
  
  """
  dp[y] ... 最大yまでの最大入れ子数
  dp[y] = max(dp[y], dp[v - 1] + 1)
  """
  dp = [0] * 1001
  for x in range(len(r_lst)):
    vlst = r_lst[x]
    max_v = 1000
    for v in vlst:
      dpv1 = dp[v - 1] + 1
      for y in range(max_v, v - 1, -1):
        if dp[y] < dpv1:
          dp[y] = dpv1
      max_v = v
  print(dp[1000])

Python: while True:
  n = int(input())
  if n == 0:
    break
  hr_lst = []
  for _ in range(n):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  m = int(input())
  for _ in range(m):
    h, r = map(int, input().split())
    hr_lst.append((h, r))

  hr_lst.sort()
  r_lst = [r for _, r in hr_lst]
  
  """
  dp[y] ... 最大yまでの最大入れ子数
  dp[y] = max(dp[y], dp[rlst[x] - 1] + 1)
  """
  dp = [0] * 1001
  for x in range(len(r_lst)):
    vx = r_lst[x]
    for y in range(vx, 1001):
      dp[y] = max(dp[y], dp[vx - 1] + 1)
  print(dp[1000])

Python: # AOJ 0157: Russian Dolls
# Python3 2018.6.19 bal4u
#   単調増加シーケンスの最長値問題（LIS）

def LIS(hr):
	n = len(hr)
	lis = [0]*n
	lis[n-1] = 1;
	for i in range(n-2, -1, -1):
		m = 0;
		for j in range(i+1, n):
			if hr[i][0] < hr[j][0] and hr[i][1] < hr[j][1] and lis[j] > m: m = lis[j]
		lis[i] = m+1
	return lis[0]

while True:
	n = int(input())
	if n == 0: break
	hr = []
	for i in range(n):
		hr.append(list(map(int, input().split())))
	for i in range(int(input())):
		hr.append(list(map(int, input().split())))
	hr = list(sorted(hr))
	print(LIS(hr))

Python: while True:
    n=int(input())
    if n==0:
        break
    dolls=[]
    for i in range(n):
        h,r=[int(j) for j in input().split(" ")]
        dolls.append((h,r))
    m=int(input())
    for j in range(m):
        h,r=[int(j) for j in input().split(" ")]
        dolls.append((h,r))

    dolls=sorted(dolls,key=lambda w:(w[0],-1*w[1]))
    r=[i[1] for i in dolls]

    table=[1 for i in range(len(r))]
    for i in range(len(r)):
        for j in range(i):
            if r[j]<r[i]:
                table[i]=max(table[i],table[j]+1)

    print(max(table))

Python: while 1:
    n = int(input())
    if n == 0:
        break
    objects = []
    for i in range(n):
        h,r = (int(x) for x in input().split())
        objects.append((h,r))
    n = int(input())
    for i in range(n):
        h,r = (int(x) for x in input().split())
        objects.append((h,r))

    objects = sorted(objects,key=lambda w:(w[0],-1*w[1]))
    r = [i[1] for i in objects]
    t = [1] * len(r)
    
    for i in range(len(r)):
        for j in range(i):
            if r[j] < r[i]:
                t[i] = max(t[i],t[j] + 1)
    print(max(t))

Python: while True:
  n = int(input())
  if n == 0:
    break
  hr_lst = []
  for _ in range(n):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  m = int(input())
  for _ in range(m):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  hr_lst.sort(reverse=True)

  r_lst = [[] for _ in range(1001)]
  for h, r in hr_lst:
    r_lst[h].append(r)
  r_lst = [lst for lst in r_lst if lst != []]
  
  """
  dp[y] ... 最大yまでの最大入れ子数
  dp[y] = max(dp[y], dp[v - 1] + 1)
  """
  dp = [0] * 1001
  for x in range(len(r_lst)):
    vlst = r_lst[x]
    max_v = 1000
    for v in vlst:
      dpv1 = dp[v - 1] + 1
      for y in range(max_v, v - 1, -1):
        if dp[y] < dpv1:
          dp[y] = dpv1
  print(dp[1000])

Python: while 1:
    N = int(input())
    if not N:
        break
    S = []
    for i in range(N):
        h, r = map(int, input().split())
        S.append((h, r))
    M = int(input())
    for i in range(M):
        h, r = map(int, input().split())
        S.append((h, r))

    S.sort()
    memo = [-1]*(N+M)
    def dfs(i):
        if memo[i] != -1:
            return memo[i]
        hi, ri = S[i]
        r = 0
        for j in range(i+1, N+M):
            hj, rj = S[j]
            if hi < hj and ri < rj:
                r = max(r, dfs(j))
        memo[i] = r+1
        return r+1
    print(dfs(0))

Python: while True:
  n = int(input())
  if n == 0:
    break
  hr_lst = []
  for _ in range(n):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  m = int(input())
  for _ in range(m):
    h, r = map(int, input().split())
    hr_lst.append((h, r))
  
  hr_lst.sort(reverse=True)

  r_lst = [[] for _ in range(1001)]
  for h, r in hr_lst:
    r_lst[h].append(r)
  r_lst = [lst for lst in r_lst if lst != []]
  
  """
  dp[y] ... 最大yまでの最大入れ子数
  dp[y] = max(dp[y], dp[v - 1] + 1)
  """
  dp = [0] * 1001
  for x in range(len(r_lst)):
    vlst = r_lst[x]
    max_v = 1000
    for v in vlst:
      for y in range(max_v, v - 1, -1):
        dp[y] = max(dp[y], dp[v - 1] + 1)
  print(dp[1000])

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main(){
  int n, m;
  int dp[201];
  P doll[200];
  while(cin >> n && n){
    fill(doll, doll+200, P(0, 0));
    fill(dp, dp + 200, INF);
    for (int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++){
      *(upper_bound(dp, dp + 201, doll[i].S)-1) = doll[i].S;
    }
    cout << lower_bound(dp, dp + 201, INF) - dp << endl;
  }
  return 0;
}