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 <iostream>

using namespace std;

int n, m;
int d[200][2], dp[200];

int main(){
    for(;;){
        if(cin >> n){
            for(int i = 0; i < 200; i++){
                dp[i] = 0;
            }
            if(n == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            cin >> m;
            for(int i = n; i < n + m; i++){
                cin >> d[i][0] >> d[i][1];
                for(int j = i; j > 0; j--){
                    if(d[j][0] > d[j - 1][0]){
                        break;
                    }else{
                        int a = d[j][0];
                        int b = d[j][1];
                        d[j][0] = d[j - 1][0];
                        d[j][1] = d[j - 1][1];
                        d[j - 1][0] = a;
                        d[j - 1][1] = b;
                    }
                }
            }
            int maax = 0;
            for(int i = 0; i < n + m; i++){
                dp[i]++;
                int a = 0;
                for(int j = 0; j < i; j++){
                    if(a <= dp[j]&&d[j][1] < d[i][1]){
                        a = dp[j];
                    }
                }
                dp[i] += a;
                if(maax <= dp[i]){
                    maax = dp[i];
                }
            }
            cout << maax << endl;
        }else{
            break;
        }
    }
    return 0;
}

C++: #include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <map>
#include <sstream>
#include <queue>
#include <cstdlib>
#include <algorithm>
#include <iterator>
#include <stack>
#include <list>
using namespace std;
#define INF 100000000
typedef long long int lli;
typedef pair<int,int> P;

bool dp[201][201];

int main(){
	int n,m;
	while(cin >> n){
		if(n==0) break;
		vector<int> h;
		vector<int> r;
		for(int i=0; i<n; ++i){
			int tmp1,tmp2;
			cin >> tmp1 >> tmp2;
			h.push_back(tmp1);
			r.push_back(tmp2);
		}
		cin >> m;
		for(int i=0; i<m; ++i){
			int tmp1,tmp2;
			cin >> tmp1 >> tmp2;
			h.push_back(tmp1);
			r.push_back(tmp2);
		}
		for(int i=0; i<=n+m; ++i){
			for(int j=0; j<n+m; ++j){
				dp[i][j]=false;
			}
		}
		for(int i=0; i<n+m; ++i){
			dp[1][i]=true;
		}
		for(int i=2; i<=n+m; ++i){
			for(int j=0; j<n+m; ++j){
				for(int k=0; k<n+m; ++k){
					if(h[k]<h[j]&&r[k]<r[j]&&dp[i-1][k]){
						dp[i][j]=true;
						break;
					}
				}
			}
		}
		int ans=0;
		for(int i=1; i<=n+m; ++i){
			for(int j=0; j<n+m; ++j){
				if(dp[i][j]) ans=max(ans,i);
			}
		}
		cout << ans << "\n";
	}
}

C++: //Solution for aoj:0157 Russian Dolls
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int>P;
const int INF = 1 << 29;

int main(){
	int n, m, a, b;
	pair<int, int> doll[201];
	int dp[201];
	while (cin >> n && n){
		fill(dp, dp + 201, INF);
		for (int i = 0; i < n; i++){
			cin >> a >> b;
			doll[i] = make_pair(a, b);
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			cin >> a >> b;
			doll[i] = make_pair(a, b);
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			if (doll[i].first == doll[i - 1].first){
				doll[i] = make_pair(0, 0);
			}
		}
		for (int i = 0; i < n + m; i++){
			*lower_bound(dp, dp + n + m, doll[i].second) = doll[i].second;
		}
		cout << lower_bound(dp, dp + n + m, INF) - dp << endl;
	}
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

int LIS(const vector<pii>& a)
{
	int size=a.size();
	if(size==0)
		return 0;
	
	vi dp(size);
	dp[0]=1;
	repi(i,1,size){
		rep(j,i){
			if(a[j].first<a[i].first && a[j].second<a[i].second)
				dp[i]=max(dp[i],dp[j]+1);
		}
	}
	return *max_element(allof(dp));
}

int main()
{
	for(int n;cin>>n,n;){
		vector<pii> a(n);
		rep(i,n) cin>>a[i].first>>a[i].second;
		int m; cin>>m;
		a.resize(n+m);
		repi(i,n,n+m)
			cin>>a[i].first>>a[i].second;
		
		sort(allof(a));
		cout<<LIS(a)<<endl;
	}
	
	return 0;
}