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 <cmath>
#include <algorithm>
using namespace std;

int n, m;
int ah[110], ar[110];
int bh[110], br[110];
int adp[110], bdp[110];

int f_b(int x);
int f_a(int x) {
  //cout << "f_a("<<x<<")\n";
  if (adp[x] != -1) return adp[x];
  int t = 0;
  for (int i=0; i<m; i++) {
    if (ah[x] <= bh[i] || ar[x] <= br[i]) break;
    t = f_b(i);
  }
  if (x > 0) t = max(t, f_a(x-1));
  return adp[x] = t + 1;
}
int f_b(int x) {
  //cout << "f_b("<<x<<")\n";
  if (bdp[x] != -1) return bdp[x];
  int t = 0;
  for (int i=0; i<n; i++) {
    if (bh[x] <= ah[i] || br[x] <= ar[i]) break;
    t = f_a(i);
  }
  if (x > 0) t = max(t, f_b(x-1));
  return bdp[x] = t + 1;
}

int main() {
  while (true) {
    cin >> n;
    if (n == 0) break;
    for (int i=0; i<n; i++) {
      cin >> ah[i] >> ar[i];
    }
    cin >> m;
    for (int i=0; i<m; i++) {
      cin >> bh[i] >> br[i];
    }

    //
    for (int i=0; i<110; i++) {
      adp[i] = -1;
      bdp[i] = -1;
    }
    adp[0] = 1;
    bdp[0] = 1;
    if (ah[0] <= bh[0] && ar[0] <= br[0]) adp[0] += 1;
    if (bh[0] <= ah[0] && br[0] <= ar[0]) bdp[0] += 1;
    cout << max(f_a(n-1), f_b(m-1)) << "\n";
  }
  return 0;
}

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

const int MAX_N = 201;
const int MAX_H = 1001;
const int MAX_R = MAX_H;
int N;
pair<int,int> E[MAX_N];
int T[MAX_H];

int solve() {
  int ans = 0;
  sort(E,E+N);
  fill(T,T+MAX_N,1);
  for(int i = 0; i < N; ++i) {
    for(int j = 0; j < N; ++j) {
      if(E[i].first > E[j].first && E[i].second > E[j].second) {
	T[i] = max(T[i], T[j] + 1);
	ans = max(ans, T[i]);
      }
    }
  }
  return ans;
}

int main() {
  while(cin >> N && N) {
    for(int i = 0; i < N; ++i) cin >> E[i].first >> E[i].second;
    int m;
    cin >> m;
    for(int i = 0; i < m; ++i) cin >> E[N+i].first >> E[N+i].second;
    N += m;
    cout << solve() << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  int n,m;
  while(true){
      cin>>n;
      if(n==0) break;
      std::vector<P> v;
      for(int i=0;i<n;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      cin>>m;
      for(int i=0;i<m;i++){
          int h,r;cin>>h>>r;
          v.push_back(P(h,r));
      }
      sort(v.begin(),v.end());
      int res=0,hmin=-1,rmin=-1;
      for(int i=0;i<(int)v.size();i++){
          int h=v[i].first,r=v[i].second;
          if(hmin<h&&rmin<r){
              rmin=r;hmin=h;
              res++;
          }
      }
      cout<<res<<endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <numeric>

#define rep2(x,from,to) for(int (x)=(from);(x)<(to);(x)++)
#define rep(x,to) rep2((x),0,(to))
#define all(c) (c).begin(),(c).end()
#define sz(v) (int)(v).size()

using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0157

int main()
{
	int n, m, h, r;
	while(cin >> n && n){
		vector<pii> dt;
		rep(i,n){
			cin >> h >> r;
			dt.push_back(pii(h,r));
		}
		cin >> m;
		rep(i,m){
			cin >> h >> r;
			dt.push_back(pii(h,r));
		}

		sort(all(dt));
		m=sz(dt);
		vector<int> dp(m+2,0);
		rep(i,m){
			dp[i]=1;
			rep(j,i) if(dt[i].second > dt[j].second) dp[i]=max(dp[i], dp[j]+1);
		}

		cout << *max_element(all(dp)) << endl;
	}
	
	
	return 0;
}

C++: #include <iostream>
#include <list>
#include <algorithm>
#define INF 10e6
#define F first
#define S second
using namespace std;

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	P dolls1[200];
	int dp[200];
	int dp1[200];
	int n,m;
	while(cin >> n,n){
		for(int i=0;i<n;i++)
			cin >> dolls[i].F >> dolls[i].S;
		cin >> m;
		for(int i=n;i<n+m;i++)
			cin >> dolls[i].F >> dolls[i].S;
		for(int i=0;i<n+m;i++)
			dolls1[i].F=dolls[i].S,dolls1[i].S=dolls[i].F;
		sort(dolls,dolls+n+m);
		sort(dolls1,dolls1+n+m);
		int cx,cy,ccx=0,ccy=0;
		fill(dp,dp+n+m,INF);
		fill(dp1,dp1+n+m,INF);
		for(int i=0;i<n+m;i++){
			if(dolls[i].F>ccx)
			*lower_bound(dp,dp+n+m,dolls[i].S)=dolls[i].S,ccx=dolls[i].F;
			if(dolls1[i].F>ccy)
			*lower_bound(dp1,dp1+n+m,dolls1[i].S)=dolls1[i].S,ccy=dolls1[i].F;
		}
		cx=lower_bound(dp,dp+n+m,INF)-dp;
		cy=lower_bound(dp1,dp1+n+m,INF)-dp1;
		cout << max(cx,cy) << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
int dp[200] = {0};
int h[200] , r[200];
int n,m;

int main(){
	while(cin >> n , n){
		rep(i,n)cin >> h[i] >> r[i];
		cin >> m;
		rep(i,m)cin >> h[n+i] >> r[n+i];
		int ans = 0;
		rep(i,200)dp[i] = 1;
		rep(i,200)rep(j,200){
			if(h[j] < h[i] && r[j] < r[i]){
				dp[i] = max(dp[j]+1,dp[i])
			}
		}
		cout << *max_element(dp,dp+n+m) << endl;
	}
}