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>
#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 = 101;
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 = 0; i < m; ++ i ) {
            cin >> H[n+i] >> R[n+i];
        }
        N = n + m;
        cout << solve( 1001, 1001 ) << endl;
    }
    return 0;
}

C++: #include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cctype>

#include <iostream>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <algorithm>

using namespace std;

#define rep(i, n) for(int i = 0; i < n; i++)
#define repi(i,l,n) for(int (i)=(int)(l);(i)<(int)(n);(i)++)
#define d_arr(arr, h, w) rep(i,(h) ){ cout << "["; rep(j,(w) ) { cout << (arr)[i][j] << ", "; } cout << "]" << endl;}
#define IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x);
#define fs first
#define sc second
#define pb push_back

typedef pair<int ,int> P;
vector<P> mv;
map<int, int> memo;

//
int next_of(int i){
    int h = mv[i].fs;
    int r = mv[i].sc;
    int nh = h;
    int nr = r;
    while(1){
        if( nh > h && nr > r) { break; }
        i++;
        nh = mv[i].fs;
        nr = mv[i].sc;
    }
    return i;
}

// mv[i] 以上で作れるマトリョーシカの数の最大値
int rec(int i){
    //cout << "rec : " << i << endl;
    if( memo.find(i) != memo.end() ){ return memo[i]; }
    if( i > mv.size()-1 ){
        return 0;
    }
    int e0 = rec(i+1);
    int next = next_of(i);
    int e1 = rec(next) + 1;

    memo[i] = max(e0,e1);
    return memo[i];
}

int main()
{
    int n,m;
    int h,r;
    while(1){
        mv.clear();
        memo.clear();
        cin >> n;
        if( n==0 ){ break; }
        rep(i,n){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        cin >> m;
        rep(i,m){
            cin >> h >> r;
            mv.pb(make_pair(h,r));
        }
        sort(mv.begin(),mv.end());
        cout << rec(0) << endl;
    }
}

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

class Doll{
public:
    int h;
    int r;
    int have;
    static bool Less(const Doll& l, const Doll& r){
	return l.h < r.h;
    }
    Doll(int x, int y, int z){
	h = x; r = y; have = z;
    }
};

typedef vector<Doll> Dolls;

int main(){
    int n, m, h, r, result=0;
    Dolls dolls;
    while(cin >> n, n){
	for(int i=0; i<n; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	cin >> m;
	for(int i=0; i<m; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	sort(dolls.begin(), dolls.end(), Doll::Less);
	
	for(Dolls::iterator it1=dolls.begin();it1 != dolls.end(); ++it1){
	    for(Dolls::iterator it2=dolls.begin();it2 != it1; ++it2){
		if(it2->h < it1->h && it2->r < it1->r && it1->have <= it2->have){
		    it1->have = it2->have + 1;
		    result = max(result, it1->have);
		}
	    }
	}
	cout << result << endl;
	dolls.clear();
	result = 0;
    }
    return 0;
}

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

typedef pair<int, int> doll;

int main()
{
	int n, m, i, j;
	vector<doll> vc;
	int dp[200];
	
	vc.reserve(200);
	
	while( cin >> n, n)
	{
		vc.clear();
		memset( dp, 0, sizeof(dp));
		
		for( i = 0; i < n; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		cin >> m;
		
		for( i = 0; i < m; i++)
		{
			doll d;
			cin >> d.first >> d.second;
			vc.push_back(d);
		}
		
		std::sort(vc.begin(), vc.end());
		
		dp[0] = 1;

		for( i = 1; i < n+m; i++)
		{
			dp[i] = 1;
			
			for( j = 0; j < i; j++ )
			{
				if( vc[j].first < vc[i].first && vc[j].second < vc[i].second )
				{
					dp[i] = max( dp[j]+1, dp[i]);
				}
				else
				{
					dp[i] = max( dp[j], dp[i]);
				}
			}
			
			cout << "dp::" << dp[i] << endl;
		}
		
		
		cout << dp[n+m-1] << endl;
	}
	
	return 0;
}

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

int main(){
	int n, m, a, b;
	P 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] = P(a, b);
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			cin >> a >> b;
			doll[i] = P(a, b);
		}
		sort(doll, doll + n + m);
		for (int i = 0; i < n + m; i++){
			if (doll[i].first != doll[i - 1].first){
				a=distance(dp,lower_bound(dp, dp + n + m, doll[i].second));
				dp[a] = doll[a].second;
			}
		}
		int ans;
		for (int i = n + m - 1; i >= 0; --i){
			if (dp[i] != INF){
				ans = i + 1;
				break;
			}
		}
		cout << ans << endl;
		//cout << lower_bound(dp, dp + n + m, INF) - dp << endl;
	}
}