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<string>
#include<algorithm>
#include<map>
#include<set>
#include<utility>
#include<cmath>
#include<cstring>
#include<queue>
#include<cstdio>
#define loop(i,a,b) for(int i=a;i<b;i++) 
#define rep(i,a) loop(i,0,a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(),in.end()
const double PI=acos(-1);
const double EPS=1e-10;
const int inf=1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;
int main(){
	int n;
	while(cin>>n,n){
		vector<pii>in;
		while(n--){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		cin>>n;
		while(n--){
			int a,b;
			cin>>a>>b;
			in.pb(pii(a,b));
		}
		sort(all(in));
		n=in.size();
		vi dp(210,1);
		loop(i,1,n){
			int ma=0;
			rep(j,i)if(in[j].first<in[i].first&&in[j].second<in[i].second)
				ma=max(ma,dp[j]);
			dp[i]+=ma;		
		}
		int ma=0;
		rep(i,n)ma=max(ma,dp[i]);
		cout<<ma<<endl;
	}
}

C++: #include<iostream>
#include<map>
#include<algorithm>
#define F first
#define S second
using namespace std;

int n, m, i;
pair < int, int > p[222];

int solve(int k, int h, int r){
  
  int ret = 0;
    for(int i=k;i<n+m;i++){
      if(h < p[i].F && r < p[i].S) ret = max(ret, solve(i+1, p[i].F, p[i].S)+1);
    }
    
    return ret;
}

int main(){
  
  while(cin >> n){

    for(i=0;i<n;i++) cin >> p[i].F >> p[i].S;
    cin >> m;  
    for(;i<m+n;i++) cin >> p[i].F >> p[i].S;
    
    sort(p, p+n+m);
    
    cout << solve(0, 0, 0) << endl;
  }

}

C++: # define _CRT_SECURE_NO_WARNINGS
# include <cstdio>
# include <algorithm>

#define MAX 100

struct doll{
	int high, r;
	doll() {}
	doll(int H, int R) : high(H), r(R) {}
	bool operator<(doll d){
		return (high < d.high) ? true : (high == d.high) ? (r < d.r) ? true : false : false;
	}
};

int n, m;

doll K[MAX * 2];
int dp[MAX * 2];

int main(){
	while (1){
		int h, r;
		scanf("%d", &n);
		if (n == 0)
			break;
		for (int i = 0; i < n; ++i){
			scanf("%d %d", &h, &r);
			K[i].high = h; K[i].r = r;
		}
		scanf("%d", &m);
		for (int i = n; i < m+n; ++i){
			scanf("%d %d", &h, &r);
			K[i].high = h;
			K[i].r = r;
		}
		std::sort(K + 1, K + 1 + n + m);
		memset(dp, 0,n+m);
		for (int i = 0; i < n + m; ++i){
			for (int j = 0; j < n + m; ++j){
				if (K[j] < K[i])
					dp[i] = std::max(dp[i], dp[j] + 1);
			}
		}
		printf("%d\n", dp[n + m-1]);
	}
	return 0;
}

C++: // AOJ 0157
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<sstream>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef pair<int,int> P;

const int NMAX = 150;

vector<P> ichiro, jiro;
int n, m;
int idp[NMAX], jdp[NMAX];
int index[NMAX], jndex[NMAX];

void init() {
  REP(i, NMAX) {
    idp[i] = jdp[i] = 0;
    index[i] = jndex[i] = 0;
  }
  jiro.clear();
  ichiro.clear();

  jiro.push_back(make_pair(0,0));
  ichiro.push_back(make_pair(0,0));
}

string str(P p) {
  stringstream ss;
  ss << "(" << p.first << "," << p.second << ")";
  return ss.str();
}

bool bigger(P big, P small) {
//  cout << str(big) << " " << str(small) << endl;
  return (big.first > small.first and big.second > small.second);
}

int lowerb(P mine, vector<P> his) {
  int len = his.size();
  for (int i = 1; i < len; i++) {
    bool f = bigger(mine,his[i]);
//    cout << (f ? "true" : "false") << endl;
    if (not f) {
//      cout << i - 1 << endl;
      return i - 1;
    }
  }
//  cout << len - 1 << endl;
  return len - 1;
}

int rec(int i, int* mine, int* his) {
//  cout << "rec(" << i << "," << mine << "," << his << ")" << endl;
  if (i < 0) throw 1;
  if (i == 0) {
    return 0;
  } else {
    return 1 + max(rec(i - 1, mine, his), rec(mine[i], his, mine));
  }
}

int solve() {
  for (int i = 1; i <= n; i++) {
    index[i] = lowerb(ichiro[i], jiro);
  }

  for (int j = 1; j <= m; j++) {
    jndex[j] = lowerb(jiro[j], ichiro);
  }

/*  REP(i, n + 1) {
    cout << index[i] << " ";
  }cout << endl;
  REP(i, m + 1) {
    cout << jndex[i] << " ";
  }cout << endl;*/

  return max(rec(n, index, jndex), rec(m, jndex, index));
}

int main() {
  while (cin >> n and n) {
    init();
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ichiro.push_back(make_pair(h, r));
    }

    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      jiro.push_back(make_pair(h, r));
    }

    cout << solve() << endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef pair<int,int> MAT;
#define h first
#define r second
#define rep(i,n) for(int i=0;i<n;i++)
int dp[1000][1000];
MAT info[200];
int n,m;

int dfs(int a,int b){
	if(~dp[a][b])return dp[a][b];
	
	int hoge = 0;
	rep(i,n){
		if(info[i].first < a && info[i].second < b){
			hoge = max(hoge, dfs(info[i].first,info[i].second)+1);
		}
	}
	return dp[a][b] = hoge;
}
int main(){
	while(cin >> n , n ){
		rep(i,1000)rep(j,1000)dp[i][j] = -1;
		
		int ret = 0;
		rep(i,n)cin >> info[i].h >> info[i].r;
		cin >> m;
		rep(i,m)cin >> info[n+i].h >> info[n+i].r;
		
		n = n + m;
		
		rep(i,n){
			ret = max(ret, dfs(info[i].first , info[i].second)+1);
		}
		cout << ret << endl;
	}
}