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<cstdio>
#define N 1000 
using namespace std;
int h1[N],r1[N];

void sort(int,int);
int syori(int,int);

int main(){/*main*/
  int i,n1,n2,j,res;

  for(i=0;i<N;i++){
    h1[i]=0;
    r1[i]=0;
  }

  while(1){

  cin >> n1; // êQÌ}g[VJÌ

  if(n1 == 0)break;

  for(i=0;i<n1;i++){
    cin >> h1[i] >> r1[i]; //(n1-1)ÜÅªêQ
  }

  cin >> n2; //ñYÌ}g[VJÌ

  for(i=n1;i<(n1+n2);i++){
    cin >> h1[i] >> r1[i]; //n1©ç(n1+n2-1)ÜÅªñY
  }

  sort(n1,n2); // ¬³¢ÉÀÑÖ¦

  //  for(i=0;i<n1+n2;i++){
  // cout << i << ":"<< h1[i] << " " << r1[i] <<endl;
  // }

   res=syori(n1,n2);

   
  
  cout << res << endl;
  res=0;

  }

}


void sort(int n1,int n2){
  int k,l,max,h;

  for(k=0;k<(n1+n2);k++){
    max=k;
    for(l=k;l<(n1+n2);l++){
      if(h1[l]<h1[max]){ max=l; }
    }
    h=h1[max];
    h1[max]=h1[k];
    h1[k]=h;

    h=r1[max];
    r1[max]=r1[k];
    r1[k]=h;
    
  }


}


int syori(int n1,int n2){
  int q,i,H[N],Hcount=0,ans,I[N],HH[N],count=1,kcount=0;
  ans=0;

  for(q=0;q<N-5;q++){  
    H[q]=0;
    I[q]=0;
    HH[q]=0;
  }

  //cout << "test" << endl;

  for(q=0;q<(n1+n2);q++){
    if(h1[q]<h1[q+1] && r1[q]<r1[q+1])count++; 
    
    if((h1[q]==h1[q+1] || r1[q] == r1[q+1]) && HH[q+1]==0 ){
      count++; 
      H[q+1]=count-1;
      HH[q+1]=1;
      Hcount++;
      I[q+1]=q+1;
      q+=1;
    }

    //  cout << Hcount << endl;


    if(q==(n1+n2-1) && Hcount!=0){
      Hcount=0;
      kcount=1;

      if(count>=ans)ans=count;
      
      // cout << "ans--" << ans << endl; 

      for(i=0;i<(n1+n2);i++){
	
	if(HH[i]==1){
	  count=H[i];
	  H[i]=0;
	  HH[i]=0;
	  q=I[i];
	  break;
	}
	
      }
      
      
    }

  
    
  }
     
  if(kcount==0){ ans=count; } // kcountªOÌÆ«Í¾ÔèÈµ
  else{ kcount=0; }           // ±ÌvOÅÍ¾ÔçÈ¢ÆansªÅÈ¢
  
    return ans;
}

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

int N, M, h[200], r[200];

int main() {
    while (true) {
        cin >> N;
        if (N == 0) break;
        for (int j = 0; j < N; ++j) {
            cin >> h[j] >> r[j];
        }
        cin >> M;
        for (int j = 0; j < M; ++j) {
            cin >> h[j+N] >> r[j+N];
        }

        int arr[1001][1001];
        for (int j = 0; j <= 1000; ++j) fill(arr[j], arr[j] + 1001, 0);

        for (int j = 0; j < N+M; ++j) {
            arr[h[j]][r[j]] = 1;
        }

        int dp[1001][1001];
        for (int j = 0; j <= 1000; ++j) fill(dp[j], dp[j] + 1001, 0);

        for (int j = 0; j <= 1000; ++j) {
            for (int k = 0; k <= 1000; ++k) {
                if (j > 0 && k > 0) {
                    dp[j][k] = max(dp[j-1][k-1] + arr[j][k], max(dp[j-1][k], dp[j][k-1]));
                } else if (j > 0) {
                    dp[j][k] = dp[j-1][k];
                } else if (k > 0) {
                    dp[j][k] = dp[j][k-1];
                }
            }
        }

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

C++: #include <bits/stdc++.h>
using namespace std;

int main() {
  while(1) {
    vector<int> a, b;
    for(int k = 0; k < 2; ++k) {
      int n;
      cin >> n;
      if(n == 0) return 0;
      for(int i = 0; i < n; ++i) {
        int c, d;
        cin >> c >> d;
        a.push_back(c);
        b.push_back(d);
      }
    }
    int n = a.size();
    int dp[n];
    for(int i = 0; i < n; ++i) dp[i] = 1;
    for(int t = 0; t < n; ++t) {
      for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
          if(a[i] < a[j] && b[i] < b[j])
            dp[j] = max(dp[j], dp[i] + 1);
        }
      }
    }
    cout << *max_element(dp, dp+n) << endl;
  }
  return 0;
}

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];
        }
      }
    }

    PP(dp);

    cout << result << endl;
  }

  return 0;
}

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

class Doll{
public:
    int h;
    int r;
    int have;
    // static bool Less(const Doll& l, const Doll& r){
    // 	return l.h < r.h;
    // }
    bool operator < (const Doll &d) const{
	if( r == d.r ) return h < d.h;
	else return r < d.r;
    }
    Doll(int x, int y, int z){
	h = x; r = y; have = z;
    }
};

typedef vector<Doll> Dolls;

int main(){
    int n, m, h, r, result=0;
    Dolls dolls;
    while(cin >> n, n){
	for(int i=0; i<n; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
	cin >> m;
	for(int i=0; i<m; ++i){
	    cin >> h >> r;
	    Doll temp(h,r,1);
	    dolls.push_back(temp);
	}
//	sort(dolls.begin(), dolls.end(), Doll::Less);
	sort(dolls.begin(), dolls.end());
	for(Dolls::iterator it1=dolls.begin();it1 != dolls.end(); ++it1){
	    for(Dolls::iterator it2=dolls.begin();it2 != it1; ++it2){
		if(it2->h < it1->h && it2->r < it1->r && it1->have <= it2->have){
		    it1->have = it2->have + 1;
		    result = max(result, it1->have);
		}
	    }
	}
	cout << result << endl;
	dolls.clear();
	result = 0;
    }
    return 0;
}