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 <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

vector<P> d;
int dp[200];

int cmp(P &a, P &b)
{
	if (a.first < b.first && a.second < b.second) return 1;
	return 0;
}

void solve()
{
	int n = d.size();
	fill(dp, dp + n, 1);
	for (int i = 1; i < n; i++){
		for (int j = 0; j < i; j++){
			dp[i] = max(dp[i], dp[j] + cmp(d[j], d[i]));
		}
	}
	int res = 0;
	for (int i = 0; i < n; i++){
		res = max(res, dp[i]);
	}
	printf("%d\n", res);
}

int main()
{
	while (true){
		d.clear();
		for (int c = 0; c < 2; c++){
			int n;
			scanf("%d", &n);
			if (!n) return 0;
			for (int i = 0; i < n; i++){
				int a, b;
				scanf("%d %d", &a, &b);
				d.push_back(P(a, b));
			}
		}
		sort(d.begin(), d.end());
		solve();
	}
}

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

typedef pair<int,int> pii;
int dptab[128][1024] = {0};

int main() {
	int n, m;
	while(cin >> n, n) {
		vector<pii> v;
		for(int i = 0; i < n; i++) {
			pii tmp;
			cin >> tmp.first >> tmp.second;
			v.push_back(tmp);
		}
		cin >> m;
		for(int i = 0; i < m; i++) {
			pii tmp;
			cin >> tmp.first >> tmp.second;
			v.push_back(tmp);
		}
		sort(v.begin(), v.end());

		for(int i = 0; i < 128; i++)
			for(int j = 0; j < 1024; j++)
				dptab[i][j] = 0;

		for(int i = 0; i < v.size(); i++) {
			for(int j = 0; j < 1024; j++) {
				if(i > 0 && v[i].first == v[i - 1].first) {
					dptab[i + 1][j]
						= max(dptab[i][j], v[i].second < j ? dptab[i - 1][v[i].second] + 1 : 0);
				}
				else {
					dptab[i + 1][j]
						= max(dptab[i][j], v[i].second < j ? dptab[i][v[i].second] + 1 : 0);
				}
			}
		}
		cout << dptab[n + m][1000] << 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 vi& 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]<a[i])
				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));
		vi r(a.size());
		rep(i,r.size())
			r[i]=a[i].second;
		cout<<LIS(r)<<endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
using namespace std;
const int MAX = 100000;
int dp[200];

int dfs(int index, vector<pair<int, int> > &mato){
	if(dp[index]) return dp[index];
	if(!index) return 1;
	int maxc = 0;
	for(int i = index-1; i >= 0; i--){
		if((mato[index].first<=mato[i].first)||(mato[index].second<=mato[i].second)) continue;
		maxc = max(maxc, dfs(i, mato));
	}
	dp[index] = maxc+1;
	return dp[index];
}

void substitute(int time, vector<pair<int, int> > &mato){
	for(int i = 0; i < time; i++){
		pair<int, int> tmp;
		cin >> tmp.first >> tmp.second;
		mato.push_back(tmp);
	}
}

int main(){
	int n, m;
	while(cin >> n && n != 0){
		fill(dp, dp+200, 0);
		vector<pair<int, int> > mato;
		substitute(n, mato);
		cin >> m;
		substitute(m, mato);
		sort(mato.begin(), mato.end());
		int maxc = 0;
		for(int i = n+m-1; i >= 0; i--) maxc = max(maxc, dfs(i, mato));
		cout << "ans:" << maxc << 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 = 101;

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

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

int main() {
    int n, m;
    while ( cin >> n && n ) {
        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( INT_MAX, INT_MAX ) << endl;
    }
    return 0;
}