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]
}

C++: //include
//------------------------------------------
#include <iostream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
using namespace std;

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>(b);i--)
#define REP(i,n)  FOR(i,0,n)

#define INF (1<<21)

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//debug
//-------------------------------------------
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

//typedef
//-------------------------------------------
typedef long long ll;
typedef pair<int, int> doll;

vector<doll> dolls;
int dp[200];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while(1) {
        int n, m;
        cin >> n;
        if (!n) break;

        dolls.clear();
        REP(i, n) {
            int h, r;
            cin >> h >> r;
            dolls.push_back(doll(h, r));
        }
        cin >> m;
        REP(i, m) {
            int h, r;
            cin >> h >> r;
            dolls.push_back(doll(h, r));
        }
        sort(dolls.begin(), dolls.end());
        memset(dp, 0, sizeof(dp));
        int res = 0;

        REP(i, dolls.size()) {
            dp[i] = 1;
            REP(j, i) {
                if (dolls[j].first < dolls[i].first && dolls[j].second < dolls[i].second) {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
                res = max(res, dp[i]);
            }
        }
        cout << res << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


const int SIZE = 201;
const int NONE = -1;

int H[SIZE];
int R[SIZE];
int N;
int dp[1002][1002];

int solve( int hh, int rr ) {
    int& res = dp[hh][rr];
    if ( res != NONE ) return res;
    int maxd = 0;
    for ( int i = 0; i < N; ++ i ) {
        if ( H[i] < hh && R[i] < rr ) {
            maxd = max( maxd, 1 + solve( H[i], R[i] ) );
        }
    }
    return res = maxd;
}

int main() {
    int n, m;
    while ( cin >> n && n ) {
        for ( int i = 0; i < 1002; ++ i ) {
            for ( int j = 0; j < 1002; ++ j ) {
                dp[i][j] = NONE;
            }
        }
        for ( int i = 0; i < n; ++ i ) {
            cin >> H[i] >> R[i];
        }
        cin >> m;
        for ( int i = n; i < n+m; ++ i ) {
            cin >> H[i] >> R[i];
        }
        N = n + m;
        cout << solve( 1001, 1001 ) << endl;
    }
    return 0;
}

C++: #include <vector>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

#define INF 999999999

int gcd(int x, int y) {
	if (x % y == 0)return y;
	else return gcd(y, x%y);
}

int lcm(int x, int y) {
	return x*y / gcd(x, y);
}
vector<pair<int, int> > mt;
vector<vector<int> > in_list;
vector<int> inside; //x番目のマトリョーシカに入る最大のマトリョーシカの数

int solve(int x) {
	if (inside[x] != -1)return inside[x];
	if (in_list[x].empty()) {
		inside[x] = 0;
		return inside[x];
	}
	int m = -1;
	for (int i = 0; i < in_list[x].size(); i++) {
		m = max(m, solve(in_list[x][i]));
	}
	inside[x] = m + 1;
	return inside[x];
}


int main() {
	while (1) {
		mt.clear();
		int n, m;
		cin >> n;
		if (n == 0)break;
		for (int i = 0; i < n; i++) {
			int h, r;
			cin >> h >> r;
			mt.push_back({ h,r });
		}
		cin >> m;
		for (int i = 0; i < m; i++) {
			int h, r;
			cin >> h >> r;
			mt.push_back({ h,r });
		}
		in_list = vector<vector<int> >(m + n, vector<int>());
		inside = vector<int>(m + n, -1);
		for (int i = 0; i < m + n; i++) {
			for (int j = 0; j < m + n; j++) {
				if (mt[i].first > mt[j].first && mt[i].second > mt[j].second) {
					in_list[i].push_back(j);
				}
			}
		}
		for (int i = 0; i < m + n; i++)solve(i);
		cout << *max_element(inside.begin(), inside.end()) + 1 << endl;
	}
	return 0;
}