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 <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int lli;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=(x);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define SZ(container) ((int)container.size())

const int INF = 1<<30;
int LIS(vector<int> v){
	vector<int> dp(v.size(), INF);
	REP(i, v.size()) *lower_bound(dp.begin(), dp.end(), v[i]) = v[i]; // upper_bound にするとstrict increaseではない部分列
	return lower_bound(dp.begin(), dp.end(), INF) - dp.begin(); // ここはlower_boundのままにする
}

int n, m;

main(){
	while(cin >> n, n){
		vector<pii> x;
		REP(i, n){
			int h, r;
			cin >> h >> r;
			x.push_back(pii(h, -r));
		}
		cin >> n;
		REP(i, n){
			int h, r;
			cin >> h >> r;
			x.push_back(pii(h, -r));
		}
		sort(ALL(x));
		vector<int> lis;
		REP(i, x.size()) lis.push_back(-x[i].second);
		cout << LIS(lis) << endl;
	}
	return 0;
}

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[2000];
  P doll[2000];
  while(cin >> n && n){
    fill(doll, doll+2000, P(0, 0));
    fill(dp, dp + 2000, 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);
    for (int i = 0; i < n + m; i++){
	*lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
    }
     int ans;
        for(int i=n+m-1; i>=0; --i){
            if(dp[i] != INF){
                ans = i + 1;
                break;
            }
        }
	cout << ans << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) begin(a),end(a)
#define MS(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(5)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<short, short> P;
typedef complex<double> Point;
typedef long long ll;
const int INF = 114514810;
const int MOD = 100000007;
const double EPS = 1e-10;
const double PI = acos(-1.0);
struct edge
{
	int from, to, cost;
	bool operator < (const edge& e) const { return cost < e.cost; }
	bool operator >(const edge& e) const { return cost > e.cost; }
};
///*************************************************************************************///
///*************************************************************************************///
///*************************************************************************************///

vector<P> v;
int n, m;
short dp[201][1001][1001];

int solve(int i, int h, int r)
{
	if (i == v.size()) return 0;
	if (dp[i][h][r] >= 0) return dp[i][h][r];
	int res;
	if (v[i].first > h&&v[i].second > r)
	{
		res = max(solve(i + 1, h, r), solve(i + 1, v[i].first, v[i].second) + 1);
	}
	else res = solve(i + 1, h, r);
	return dp[i][h][r] = res;
}

int main()
{
	while (cin >> n, n)
	{
		v.clear();
		MS(dp, -1);
		REP(i, n)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, b));
		}
		cin >> m;
		REP(i, m)
		{
			int a, b; cin >> a >> b;
			v.push_back(P(a, b));
		}
		sort(ALL(v));
		cout << solve(0, 0, 0) << endl;
	}
	return 0;

}

C++: #include <bits/stdc++.h>
#define REP(i,n) for(int i=0; i<n; i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define ALL(c) (c).begin(), (c).end() 
using namespace std;

struct doll {
	int h, r; // h, r <= 1,000
	doll() {}
	bool operator<(const doll& right) const {
		return h == right.h ? r < right.r : h < right.h;
	}
	bool bigger_than(const doll d) {
		return h > d.h && r > d.r;
	}
};

#define MAX 100

int n, m; // n, m <= 100
vector<doll> dolls;
int dp[MAX * 2]; // dp[i]: i??????????????¢?????????????????£????????????

int main()
{	
	int maxval, i;
/************** input **************/
	while(cin >> n, n) {
		for(i = 0; i < n; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
		cin >> m;
		for(i = 0; i < m; i++) {
			doll tmp;
			cin >> tmp.h >> tmp.r;
			dolls.push_back(tmp);
		}
/************** solve **************/
		sort(ALL(dolls));
#if 0
		for(int i = 0; i < n + m; i++)
			cout << dolls.at(i).h << " " << dolls.at(i).r << endl;
#endif
		for(i = 0; i < n + m; i++) {
			maxval = 0;
			for(int j = i - 1; j >= 0; j--) 
				if(dolls.at(i).bigger_than(dolls.at(j)))
					maxval = max(maxval, dp[j]);
			dp[i] = maxval + 1;
		}
/************** output **************/
		maxval = 0;
		for(i = n + m; i >= 0; i--)
			maxval = max(maxval, dp[i]);
		cout << maxval << endl;
	}
}