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 <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 <bits/stdc++.h>
using namespace std;
const int dx[]={0,1,0,-1,1,-1,-1,1};
const int dy[]={-1,0,1,0,1,1,-1,-1};
const int INF = 1<<30;
const double EPS = 1e-15;
#define PB push_back
#define mk make_pair
#define fi first
#define se second
#define ll long long
#define reps(i,j,k) for(int i = (j); i < (k); i++)
#define rep(i,j) reps(i,0,j)
#define MOD 1000000007
typedef pair<int,int> Pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
vector < Pii > data;
int N,M;
int memo[1280][1280];
int solve(int dep,int prev1,int prev2){
	if(dep == N+M){
		return 0;
	}
	if(memo[prev1][prev2])return memo[prev1][prev2];
	int ret = 0;
	reps(i,dep,N+M){
		if(prev1 < data[i].fi && prev2 < data[i].se){
			ret = max(ret ,1 + solve(i+1,data[i].fi,data[i].se));
		}
	}
	return memo[prev1][prev2] = ret;
}
int main(){
	while(scanf("%d",&N),N){
		data.clear();
		memset(memo,0,sizeof(memo));
		rep(i,N){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		scanf("%d",&M);
		rep(i,M){
			int a,b;
			scanf("%d%d",&a,&b);
			data.PB(Pii(a,b));
		}
		sort(data.begin(),data.end());
		printf("%d\n",solve(0,0,0));
	}

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <sstream>
#include <fstream>
//#include <tuple>
#include <set>
#include <string.h>

#define X first
#define Y second
#define MP make_pair
//#define MT make_tuple
#define REP(i, a, n) for(int (i) = (a); (i) < (n); ++(i))
#define FOR(i, n) REP(i, 0, n)
typedef long long ll;
using namespace std;
const int MAX = 101;

template<class T, class U>
void convert(T &t, U &u){
	stringstream ss;
	ss << t;
	ss >> u;
}

class a{
public:

};

int count(vector<pair<int, int> >& a){
	int ans = 0;
	pair<int, int> tmp;
	FOR(i, a.size()){
		if (i){
			if (tmp.first < a[i].first){
				if (tmp.second < a[i].second){
					tmp = a[i];
					ans++;
				}
			}
		}
		else tmp = a[i], ans++;
	}

	return ans;
}

int main(){

	int n;
	while (cin >> n && n){
		vector<pair<int, int> > a, b;
		FOR(i, n){
			int s, t; cin >> s >> t;
			a.push_back( MP( s, t ) );
			b.push_back( MP( t, s ) );
		}

		int m; cin >> m;
		FOR(i, m){
			int s, t; cin >> s >> t;
			a.push_back( MP( s, t ) );
			b.push_back( MP( t, s ) );
		}

		sort( a.begin(), a.end() );
		sort( b.begin(), b.end() );
		cout << max( count( a ), count( b ) ) << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<stdlib.h>
#include<cstdlib>
using namespace std;

struct ppp {
  int h;
  int r;
};

int tansaku(vector<ppp> c, vector<ppp> d, int cN, int dN, int ans, int cy, int dy) {
  if(dN >= d.size()) {
    for(int i = cN; i < c.size(); i++) {
      if(d[dy].r < c[i].r && d[dy].h < c[i].h) {
        ans++;
        d[dy] = c[i];
      }
    }
    return ans;
  }
  if(cN >= c.size()) {
    for(int i = dN; i < d.size(); i++) {
      if(d[i].r > c[cy].r && d[i].h > c[cy].h) {
        ans++;
        c[cy] = d[i];
      }
    }
    return ans;
  }
  if(c[cN].h == d[dN].h) {
    return c[cN].r > d[dN].r ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].r == d[dN].r) {
    return c[cN].h > d[dN].h ? tansaku(c, d, cN + 1, dN + 1, ans + 1, cy, dN) :
    tansaku(c, d, cN + 1, dN + 1, ans + 1, cN, dy);
  }
  if(c[cN].h < d[dN].h && c[cN].r < d[dN].r) {
    return tansaku(c, d, cN + 1, dN, ans + 1, cN, dy);
  }
  if(c[cN].h > d[dN].h && c[cN].r > d[dN].r) {
    return tansaku(c, d, cN, dN + 1, ans + 1, cy, dN);
  }
  if((c[cN].h < d[dN].h && c[cN].r > d[dN].r) || (c[cN].h > d[dN].h && c[cN].r < d[dN].r)) {
    return max(tansaku(c, d, cN + 1, dN, ans + 1, cN, dy), tansaku(c, d, cN, dN + 1, ans + 1, cy, dN));
  }
  return ans;
}

int main() {
  int a, b;
  cin >> a;
  for(int j = 0; a != 0; j++) {
    vector<ppp> c(a);
    for(int i = 0; i < a; i++) {
      cin >> c[i].h >> c[i].r;
    }
    cin >> b;
    vector<ppp> d(b);
    for(int i = 0; i < b; i++) {
      cin >> d[i].h >> d[i].r;
    }
    cout << tansaku(c, d, 0, 0, 0, 0, 0) << endl;
    cin >> a;
  }
}