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<bits/stdc++.h>
#define mk make_pair
#define f first
#define s second
using namespace std;
int n,m,p1,p2,dp[201][1001][1001];
unordered_map<pair<int,pair<int,int> >,int >m1;

vector<pair<int,int> >v;

int dfs(int d,int h,int w){
  int res=0;
  if(m1[mk(d,mk(h,w))])return m1[mk(d,mk(h,w))];
  if(d==v.size()) return 0;
  if(v[d].f<h&&v[d].s<w)
    res=max(res,dfs(d+1,v[d].f,v[d].s))+1;
  res=max(res,dfs(d+1,h,w));
  return m1[mk(d,mk(h,w))]=res;
}

main(){
  while(cin>>n,n){
    m1.clear();
    v.clear();
    for(int i=0;i<n;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    cin>>m;
    for(int i=0;i<m;i++){
      cin>>p1>>p2;
      v.push_back(mk(p2,p1));
    }
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    for(int i=0;i<v.size();i++)swap(v[i].f,v[i].s);
    stable_sort(v.begin(),v.end(),greater<pair<int,int> >());
    cout<<dfs(0,1001,1001)<<endl;
  }
}

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

struct DOLL{
	int h;
	int r;
};

int memo[1001][1001];

int f(vector<DOLL> &d1, vector<DOLL> &d2, int h, int r,int i1,int i2)
{
	if( memo[r][h] >= 0 ){
		return memo[r][h];
	}


	int ans = 0;

	for(int i=i1;i<d1.size();i++){
		DOLL &d=d1[i];
		if( d.r>r && d.h>h ){
			int a = f(d1,d2,d.h,d.r,i+1,i2)+1;
			ans = max(a,ans);
			break;
		}
	}

	for(int i=i2;i<d2.size();i++){
		DOLL &d=d2[i];
		if( d.r>r && d.h>h ){
			int a = f(d1,d2,d.h,d.r,i1,i2+1)+1;
			ans = max(a,ans);
			break;
		}
	}

	memo[r][h] = ans;	

	return ans;
}

int main() {

	while (true){

		for(int i=0;i<1001;i++){
			fill(memo[i],memo[i]+1001,-1);
		}

		vector<DOLL> d1;
		vector<DOLL> d2;
		int n,m;

		cin >> n;
		if (n == 0) break;

		for (int i = 0; i<n; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d1.push_back(d);
		}

		cin >> m;

		for (int i = 0; i<m; i++){
			DOLL d;
			cin >> d.h >> d.r;
			d2.push_back(d);
		}

		int ans = f(d1, d2, 0, 0,0,0);
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
  int n;
  for(;cin>>n,n;)
    {
      vector<pair<int,int> >data;
      for(int i=0;i<n;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	}
      int m;
      cin>>m;
      for(int i=0;i<m;i++)
	{
	  int h,r;
	  cin>>h>>r;
	  data.push_back(make_pair(h,r));
	}
      sort(data.begin(),data.end());
      /*      for(int i=0;i<data.size();i++)
	cout<<"<"<<data[i].first<<","<<data[i].second<<"> ";
	cout<<endl;
      */
      int ans=1;
      for(int j=data.size()-1;j>=0;j--)
	{
	  int tmp=1;
	  pair<int,int >last=data[j];
	  for(int i=j-1;i>=0;i--)
	    {
	      // cout<<"<"<<last.first<<","<<last.second<<"> "<<endl;
	      if(last.first>data[i].first &&
		 last.second>data[i].second
		 )
		{
		  tmp++;
		  last=data[i];
		}
	    }
	  if(tmp>ans)
	    ans=tmp;
	}
      cout<<ans<<endl;
    }

}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<n;i++)
#define PP(m) REP(i, m.size()) cout << m[i] << endl;


struct Ma {
  int h,r;

  bool operator<( const Ma& right ) const {
    return h == right.h ? r < right.r : h < right.h;
  }
};

int main() {
  int n, m;
  while (cin >> n && n) {

    vector<Ma> ich(n);
    REP(i, n) cin >> ich[i].h >> ich[i].r;
    cin >> m;
    vector<Ma> jir(m);
    REP(i, m) cin >> jir[i].h >> jir[i].r;

    vector<Ma> mas(n+m);
    REP(i, n) mas[i] = ich[i];
    REP(i, m) mas[i+n] = jir[i];

    sort(mas.begin(), mas.end());

    vector<int> dp(n+m, 1);

    int result = 0;
    REP(i, n+m) {
      REP(j, n+m) {
        if (mas[i].h < mas[j].h && mas[i].r < mas[j].r) {
          dp[j] = max(dp[j], dp[i]+1);
        }
        if (result < dp[j]) {
          result = dp[j];
        }
      }
    }

    cout << result << endl;
  }

  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <utility>

int N, M;
std::pair<int, int> Mat[201];
int dp[201];

int main(){
    while(scanf("%d", &N), N){
        memset(dp, -1, sizeof(dp));
        
        for(int i = 0; i < N; ++i)
            scanf("%d %d", &Mat[i].first, &Mat[i].second);
    
        scanf("%d", &M);
        
        for(int i = 0; i < M; ++i)
            scanf("%d %d", &Mat[i + N].first, &Mat[i + N].second);
        
        std::sort(Mat, Mat + (M + N));
        
        for(int i = 0; i < N + M; ++i)
            dp[i] = 1;
        
        int res = 1;
        for(int i = 0; i < N + M; ++i){
            for(int j = 0; j < i; ++j){
                if(Mat[i].first > Mat[j].first && Mat[i].second > Mat[j].second){
                    dp[i] = std::max(dp[i], dp[j] + 1);
                    
                    res = std::max(res, dp[i]);
                }
            }
        }
        
        std::cout << res << std::endl;
        
    }
    
    return 0;
}

C++: #include<iostream>

using namespace std;

int main(void){
  int n,m,h[201],r[201];

  while(cin >> n,n){
    for(int i=0;i<n;i++)cin >> h[i] >> r[i];
    cin >>m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(h[i]>h[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }
    for(int i=0;i<n+m;i++){
      for(int j=i;j<n+m;j++){
	if(r[i]>r[j]){
	  int t=h[i];
	  h[i]=h[j];
	  h[j]=t;
	  t=r[i];
	  r[i]=r[j];
	  r[j]=t;
	}
      }
    }

    int dp[201]={0};
    dp[0]=1;
    for(int i=0;i<n+m-1;i++){
      for(int j=i+1;j<n+m;j++){
	if(h[i]<h[j] && r[i]<r[j] && dp[i]>=dp[j])dp[j]=dp[i]+1;
      }
    }
    int res=0;
    for(int i=0;i<n+m;i++)res=max(res,dp[i]);
    cout << res << endl;
  }

  return 0;
}

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

int main(){
  int n,m;
  int h[200],r[200],dp[200];

  while(1){
    cin >> n;
    if(!n)break;

    for(int i=0;i<n;i++)cin >> h[i] >> r[i];

    cin >> m;
    for(int i=n;i<n+m;i++)cin >> h[i] >> r[i];

    for(int i=0;i<n+m;i++)
      for(int j=i+1;j<n+m;j++)
	if(h[i]>h[j] || (h[i]==h[j] && r[i]>r[j])){
	  swap(h[i],h[j]);
	  swap(r[i],r[j]);
	}
   
    int ans = 0;
    for(int i=1;i<n+m;i++)dp[i] = 1;
    dp[0] = 1;
    for(int i=1;i<n+m;i++){
      for(int j=0;j<i;j++)
	if(h[i]>h[j] && r[i]>r[j])dp[i] = max(dp[i],dp[j]+1);
      ans = max(ans,dp[i]);
    }
    
    cout << ans << endl;
  }
}