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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


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

typedef struct{
  int h,r;
}mat;

bool lessh(const mat&l,const mat&r){return l.h<r.h;};
bool lessr(const mat&l,const mat&r){return l.r<r.r;};


int low[200],ans;
mat hr[200];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>hr[i].h>>hr[i].r;
    int m;
    cin>>m;
    rep(i,m)cin>>hr[i+n].h>>hr[i+n].r;

    
    memset(low,0,sizeof(0));

    sort(hr,hr+m+n,lessh);
    stable_sort(hr,hr+m+n,lessr);

    int ans=0;
    rep(i,n+m){
      rep(j,i){
	if(hr[j].h<hr[i].h){
	  low[i]=max(low[i],low[j]+1);
	  ans=max(ans,low[i]);
	}
      }
    }

    cout<<ans+1<<endl;
  }
}

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


class doll;


int n, m;
vector<doll> d;
vector<int> dp = { 0 };


class doll{
public:
	int h;
	int r;
	doll(int h, int r) : h(h), r(r){}
	int operator[](char* member){
		if (member == "h") return h;
		else if(member == "r") return r;
		else return -1;
	}
};


int solve(char* member){
	dp[0] = d[0][member];
	for (int i = 1; i < d.size(); i++){
		if (dp.back() < d[i][member]){
			dp.push_back(d[i][member]);
		}
		else{
			*lower_bound(dp.begin(), dp.end(), d[i][member]) = d[i][member];
		}
	}
	return dp.size();
}


template<typename T>
void clear(vector<T>& v){
	vector<T> emp;
	swap(v, emp);
}


int main(){
	while (cin >> n, n != 0){
		for (int i = 0; i < n; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}
		cin >> m;
		for (int i = n; i < n + m; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.h < d2.h);
		});

		int ans = solve("r");

		clear(dp);
		dp.push_back(0);

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.r < d2.r);
		});

		ans = max(ans, solve("h"));

		cout << ans << endl;

		clear(d);
		clear(dp);
		dp.push_back(0);
	}

	return 0;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  int n,m;
  int i,j;
  int h,r;

  while(1){

    int doll[1005][1005]={};
    int dp[1005][1005]={};

    scanf("%d",&n);
    if(n==0)break;
    rep(i,n){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    scanf("%d",&m);
    rep(i,m){
      scanf("%d %d",&h,&r);
      doll[h][r]=1;
    }

    int b=1;

    for(i=1;i<1001;i++)
      for(j=b;j<1001;j++){
	dp[i][j]=dp[i-1][j]+doll[i][j];
	if(doll[i][j]){
	  for(int k=j+1;k<1001;k++)
	    dp[i][k]=dp[i][j];
	  b=j+1;
	  break;
	}
      }

    cout<<dp[1000][1000]<<endl;
  }
}

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

using namespace std;

int main(){
    int n, m;
    while(cin >> n, n){
        vector<pair<int, int> > v;
        for(int i=0; i< n; i++){
            int h, r;
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        cin >> m;
        for(int i=0; i< m; i++){
            int h, r;
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        sort(v.begin(), v.end());
        int count = 0;
        int dp[200] = {};
        for(int i=0; i < v.size(); i++){
            for(int j=0; j < i; j++){
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    dp[i] = max(dp[i], dp[j]+1);
                }
            }
            count = max(dp[i], count);
        }
        cout << count+1 << endl;
    }
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <sstream>
#include <cassert>
#include <list>
using namespace std;
static const double EPS = 1e-5;
typedef long long ll;
typedef pair<int,int> PI;
typedef vector<int> vi;
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
#define ALL(c) (c).begin(), (c).end()
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define F first
#define S second


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

typedef struct{
  int h,r;
}mat;

bool lessh(const mat&l,const mat&r){return l.h<r.h;};
bool lessr(const mat&l,const mat&r){return l.r<r.r;};


int low[200],ans;
mat hr[200];

main(){
  int n;
  while(cin>>n,n){
    rep(i,n)cin>>hr[i].h>>hr[i].r;
    int m;
    cin>>m;
    rep(i,m)cin>>hr[i+n].h>>hr[i+n].r;

    
    memset(low,0,sizeof(0));

    stable_sort(hr,hr+m+n,lessh);
    stable_sort(hr,hr+m+n,lessr);


    int ans=0;
    rep(i,n+m){
      rep(j,n+m){
	if(hr[j].h<hr[i].h && hr[j].r<hr[i].r){
	  low[i]=max(low[i],low[j]+1);
	  ans=max(ans,low[i]);
	}
      }
    }
    //rep(i,m+n)cout<<hr[i].h<<" "<<hr[i].r<<" "<<low[i]+1<<endl;
    cout<<ans+1<<endl;
  }
}