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++: // AOJ 0157
#include<iostream>
#include<map>
#include<vector>
#include<string>
#include<sstream>
#define REP(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
typedef pair<int,int> P;

const int NMAX = 150;

vector<P> ichiro, jiro;
int n, m;
int idp[NMAX], jdp[NMAX];
int index[NMAX], jndex[NMAX];
map<pair<int,int*>, int> table;

void init() {
  REP(i, NMAX) {
    idp[i] = jdp[i] = 0;
    index[i] = jndex[i] = 0;
  }
  jiro.clear();
  ichiro.clear();

  jiro.push_back(make_pair(0,0));
  ichiro.push_back(make_pair(0,0));
  table.clear();
}

bool bigger(P big, P small) {
  return (big.first > small.first and big.second > small.second);
}

int lowerb(P mine, vector<P> his) {
  int len = his.size();
  for (int i = 1; i < len; i++) {
    if (not bigger(mine, his[i])) {
      return i - 1;
    }
  }
  return len - 1;
}

int rec(int i, int* mine, int* his) {
  auto it = table.find(make_pair(i, mine));
  if (it != table.end()) {
    return it->second;
  }
  if (i == 0) {
    return 0;
  } else {
    int ans = 1 + max(rec(i - 1, mine, his), rec(mine[i], his, mine));
    table.insert(make_pair(make_pair(i, mine), ans));
    return ans;
  }
}

int solve() {
  for (int i = 1; i <= n; i++) {
    index[i] = lowerb(ichiro[i], jiro);
  }

  for (int j = 1; j <= m; j++) {
    jndex[j] = lowerb(jiro[j], ichiro);
  }

  return max(rec(n, index, jndex), rec(m, jndex, index));
}

int main() {
  while (cin >> n and n) {
    init();
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ichiro.push_back(make_pair(h, r));
    }

    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      jiro.push_back(make_pair(h, r));
    }

    cout << solve() << endl;
  }
}

C++: #include<stdio.h>
#include<list>
#include<utility>
#include<algorithm>
using namespace std;


typedef list<int> OuterList;
struct Doll
{
	int outerCount;	
	int h, r;
	OuterList outerList;
};
const int N = 200;
Doll doll[N];

bool SortDoll(const Doll &lhv, const Doll &rhv)
{
	return (lhv.h+lhv.r) > (rhv.h+rhv.r);
}
bool CmpOuterDoll(int lhv, int rhv)
{
	return doll[lhv].outerCount < doll[rhv].outerCount;
}

int main()
{
	int n,m,i,j,dollCount;
	const int INVALID_VALUE = -1;

	while(scanf("%d",&n),n)
	{
		for(i = 0; i < n; ++i)
			scanf("%d%d",&doll[i].h, &doll[i].r);
		scanf("%d",&m);
		for(i = 0; i < m; ++i)
			scanf("%d%d",&doll[i+n].h, &doll[i+n].r);

		dollCount = n+m;
		sort(doll, doll+dollCount, SortDoll);

		for(i=0; i < dollCount; ++i)
		{
			doll[i].outerCount = INVALID_VALUE;
			doll[i].outerList.clear();
			for(j = 0; j < dollCount; ++j)
			{
				if(doll[i].h < doll[j].h &&
					doll[i].r < doll[j].r)
				{
					doll[i].outerList.push_back(j);
				}
			}
		}

		int finishCount = 0;
		int maxDoll = 0;
		while(finishCount < dollCount)
		{
			for(i = 0; i < dollCount; ++i)
			{
				if(doll[i].outerCount == INVALID_VALUE)
				{
					bool f = true;for(OuterList::iterator ite = doll[i].outerList.begin();
						ite != doll[i].outerList.end(); ++ite)
					{
						if(doll[*ite].outerCount == INVALID_VALUE)
						{
							f = false;
							break;
						}
					}
					if(f)
					{
						if(doll[i].outerList.empty())
						{
							doll[i].outerCount = 1;
						}
						else
						{
	doll[i].outerCount = doll[
								*max_element(
									doll[i].outerList.begin(), doll[i].outerList.end(), CmpOuterDoll
								)
								].outerCount + 1;
						}
						maxDoll = max(maxDoll, doll[i].outerCount);
					}
				}
			}
		}

		printf("%d\n", maxDoll);
	}
	return 0;
}

C++: //28
#include<iostream>
#include<utility>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
  for(int n;cin>>n,n;){
    vector<pair<int,int> > v;
    for(int i=0;i<n;i++){
      pair<int,int> p;
      cin>>p.first>>p.second;
      v.push_back(p);
    }
    int m;
    cin>>m;
    for(int i=0;i<m;i++){
      pair<int,int> p;
      cin>>p.first>>p.second;
      v.push_back(p);
    }
    sort(v.begin(),v.end());
    int ans=0;
    int ma[200];
    fill_n(ma,200,1<<30);
    for(int i=0;i<v.size();i++){
      int c=lower_bound(ma,ma+200,v[i].second)-ma;
      ans=max(ans,c);
      ma[c]=v[i].second;
    }
    cout<<ans+1<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<vector>
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 <fstream>
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define INF 1e8
#define EPS 1e-9

#define rep2(i,m,n) for(int i=m;i<n;i++)
#define rep(i,n) rep2(i,0,n)
#define ll long long
#define P pair<int,int>
#define pb push_back

int n,m,h[200],r[200],d[200][200];

int main(){
	while(cin>>n&&n){
		rep(i,n)cin>>h[i]>>r[i];
		cin>>m;
		rep(i,m)cin>>h[i+n]>>r[i+n];
		n+=m;
		rep(i,n+m)fill(d[i],d[i]+n+m,INF);
		rep(i,n)rep(j,n){
			if(i==j)d[i][j]=0;
			if(h[i]>h[j]&&r[i]>r[j])d[i][j]=-1;
		}
		rep(k,n)rep(i,n)rep(j,n)d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
		int ans=0;
		rep(i,n)rep(j,n)ans=max(ans,-d[i][j]+1);
		cout<<ans<<endl;
	}
}