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 <algorithm>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;
const int INF = 1 << 30;
int main(){
  int n, m;
  int dp[2000];
  P doll[2000];
  while(cin >> n && n){
    fill(doll, doll+2000, P(0, 0));
    fill(dp, dp + 2000, INF);
    for (int i = 0; i < n; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    cin >> m;
    for (int i = n; i < n + m; i++){
      int a, b;
      cin >> a >> b;
      doll[i] = P(a, b);
    }
    sort(doll, doll + n + m);
    dp[0] = doll[0].S;
    for (int i = 1; i < n + m; i++){
	*lower_bound(dp, dp + 2000, doll[i].S) = doll[i].S;
    }
     int ans;
        for(int i=n+m-1; i>=0; --i){
            if(dp[i] != INF){
                ans = i + 1;
                break;
            }
        }
	cout << ans << endl;
  }
  return 0;
}

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

typedef pair<int,int> P;

int main(void){
	P dolls[200];
	P dolls1[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 coux=0,couy=0,cx=0,cy=0;
		for(int i=0;i<n+m;i++)
			if(cx<dolls[i].F&&cy<dolls[i].S)
				cx=dolls[i].F,cy=dolls[i].S,coux++;
		cx=0,cy=0;
		for(int i=0;i<n+m;i++)
			if(cx>dolls1[i].F&&cy>dolls1[i].S)
				cx=dolls1[i].F,cy=dolls1[i].S,couy++;
		cout << max(coux,couy) << endl;
	}
	return 0;
}

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

pair<int,int> D[220];

int N, M, h, r;

int main() {

	while ( cin >> N && N ) {

		for ( int i = 0; i < N; i++) {

			cin >> h >> r;
			D[i] = make_pair(h, r);

		}

		cin >> M;

		for ( int i = 0; i < M; i++) {

			cin >> h >> r;
			D[N+i] = make_pair(h, r);

		}

		sort(D,D+N+M);

		int A[200];
		
		A[0] = 0;

		for ( int i = 0; i < N + M; i++) {

			int maxaj = 0;
			
			for ( int j = 0; D[j].first < D[i].first; j++ ) {
			
				if ( D[j].second < D[i].second ) {
				
					maxaj = max( maxaj, A[j] );
				
				}
			
			}
			
			A[i] = 1 + maxaj;

		}
		
		int maxai = 0;

		for ( int i = 0; i < N + M; i++) {
			
			maxai = max( maxai, A[i] );
				
		}
		
		cout << maxai << endl;

	}

}

C++: #include <stdio.h>
#include <utility>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> pi;
#define F first
#define S second
#define MP make_pair
#define PB push_back
const int N=1e3+10;
int main(){
    int n,ans,d[N];
    pi temp;
    vector<pi> v;
    while(true){
        scanf("%d",&n);
        if(n==0)return 0;
        v.clear();
        ans=0;
        while(n--){
            scanf("%d%d",&temp.F,&temp.S);
            v.PB(temp);
        }
        scanf("%d",&n);
        while(n--){
            scanf("%d%d",&temp.F,&temp.S);
            v.PB(temp);
        }
        sort(v.begin(),v.end());
        for(int i=0;i<v.size();i++){
            d[i]=1;
            for(int j=0;j<i;j++)if(v[i].F>v[j].F&&v[i].S>v[j].S)d[i]=max(d[i],d[j]+1);
            ans=max(ans,d[i]);
        }
        printf("%d\n",ans);
    }
}

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

int h[201],r[201];
int num[201];


void LIS(int n,int m){

  int max;

  h[0]=0;
  r[0]=0;
  num[0]=0;
  
  for(int i=1 ; i <= m+n ; i++){
    max=0;
    for(int j=0 ; j < i ; j++){
      if(r[j] < r[i] && h[j] < h[i] && max < num[j])max=num[j];
    }
    num[i]=max+1;
  }
  
  /*cout <<endl;
  for(int i=0 ; i <= m+n ; i++){
    cout <<num[i]<<endl;
  }
  */

  max=num[0];
  for(int i=0 ; i <= n+m ; i++){
    if(max < num[i])max=num[i];
  }

  cout <<max<<endl;
	
  
}
int main(){

  int n,m;

  while(1){
    
    cin >>n;
    if(n == 0)break;
    
    for(int i=1 ; i <= n ; i++){
      cin >>h[i]>>r[i];
    }
    
    cin >>m;
    for(int i=n+1 ; i <= m+n ; i++){
      cin >>h[i]>>r[i];
    }
    
    for(int i=n+m ; i > 1 ; i--){
      for(int j=1 ; j < i ; j++){
	if(h[j] > h[j+1]){
	  swap(h[j],h[j+1]);
	  swap(r[j],r[j+1]);
	}else if(h[j] == h[j+1] && r[j] > r[j+1]){
	  swap(r[j],r[j+1]);
	}
      }
    }
    
    //cout <<endl;
    //for(int i=1 ; i <= n+m ; i++){
    //  cout <<h[i]<<" "<<r[i]<<endl;
    //}
    
    LIS(n,m);

  }
  return 0;
}

C++: #include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<cassert>
#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<utility>
#include<numeric>
#include<algorithm>
#include<bitset>
#include<complex>
#include<stack>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<string> vstring;

struct Edge{int to,from,cost;};

template<class T> void chmin(T &t, T f) { if (t > f) t = f; }
template<class T> void chmax(T &t, T f) { if (t < f) t = f; }
int in() { int x; scanf("%d", &x); return x; }

#define rep(i,n) for(int i=0;i<(n);++i)
#define rep0(i,n) for(i=0;i<(n);++i)
#define repn(i,m,n) for(int i=(m);i<=(n);++i)
#define all(n) n.begin(),n.end()
#define sz(n) ((int)(n).size())
#define mp make_pair
#define PUTLINE cout<<"LINE:"<<__LINE__<<endl;

const int INF = 2147483647;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[]={1,-1,0,0,1,-1,1,-1,0};
const int dy[]={0,0,1,-1,1,-1,-1,1,0};

vector<pint> s;
int mem[210];
int n,m;

int f(int k){
	if(mem[k]>-1)return mem[k];
	int res=0;
	rep(i,n+m){
		if(s[k].first>s[i].first&&s[k].second>s[i].second)chmax(res,f(i)+1);
	}
	return mem[k]=res;
}

int main() {
	for(;;){
		cin>>n;
		if(n==0)break;
		s.clear();
		rep(i,n){
			pint buf;
			cin>>buf.first>>buf.second;
			s.push_back(buf);
		}
		cin>>m;
		rep(i,m){
			pint buf;
			cin>>buf.first>>buf.second;
			s.push_back(buf);
		}
		memset(mem,-1,sizeof(mem));
		rep(i,n+m)f(i);
		int res=0;
		rep(i,n+m)chmax(res,mem[i]);
		cout<<res+1<<endl;
	}
	return 0;
}