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>
#include <string>
#include <fstream>

struct MT {
public:
	int w;
	int h;
};

int max(int a, int b) {
	return a > b ? a : b;
}

int main() {
	std::ifstream ifs("input.txt");
	do {
		int n, m;
		std::cin >> n;
		std::vector<MT> mtA;
		if (n == 0) break;
		for (int i = 0; i < n; i++) {
			int w, h;
			std::cin >> w;
			std::cin >> h;
			MT m;
			m.w = w;
			m.h = h;
			mtA.push_back(m);
		}

		std::cin >> m;
		for (int i = 0; i < m; i++) {
			int w, h;
			std::cin >> w;
			std::cin >> h;
			MT m;
			m.w = w;
			m.h = h;
			mtA.push_back(m);
		}

		std::sort(mtA.begin(), mtA.end(), [](const MT& lhs, const MT& rhs){return lhs.w < rhs.w; });

		/*
		std::cout << "testtest" << std::endl;
		for (int i = 0; i < n + m; i++) {
			std::cout << mtA[i].w << std::endl;
		}
		std::cout << "endtest" << std::endl;
		*/

		std::vector<int> paths(n + m);
		int minw = mtA[0].w;
		int mini;
		for (int i = 0; i < n + m; i++) {
			if (mtA[i].w == minw) {
				paths[i] = 1;
			}
			else {
				mini = i;
				break;
			}
		}

		for (int i = mini; i < n + m; i++) {
			for (int j = 0; mtA[j].w != mtA[i].w; j++) {
				if (mtA[j].h < mtA[i].h) {
					paths[i] = max(paths[i], paths[j] + 1);
					//std::cout << "i:" << i << "j:" << j << " paths[i]:" << paths[i] << " paths[j]:" << paths[j] << std::endl;
				}
			}
		}
		int maxpath = 0;
		for (int i = 0; i < n + m ; i++ ) {
			maxpath = max(maxpath, paths[i]);
		}
		std::cout << maxpath << std::endl;

	} while (1);


	return 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[201], dp2[201];
  P doll[200];
  while(cin >> n && n){
    fill(doll, doll+200, P(0, 0));
    fill(dp, dp + 201, INF);
    fill(dp2, dp + 201, 0);
    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++){
      int *p;
      p = lower_bound(dp, dp + 201, doll[i].S);
	if(doll[i].F != dp2[p-1-dp]){
	  *p = doll[i].S;
	  dp2[p-dp]=doll[i].F;
	}
    }
    cout << lower_bound(dp, dp + 201, INF) - dp << endl;
  }
  return 0;
}

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 <= 200; ++j) fill(arr[j], arr[j] + 201, 0);

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

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

        for (int j = 0; j <= 200; ++j) {
            for (int k = 0; k <= 200; ++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[200][200] << endl;
    }
    return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init));

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<pll> v;
    ll h,r;
    rep(i,n){
      cin >> h >> r;
      v.push_back(make_pair(h,r));
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      v.push_back(make_pair(h,r));
    }
    ll ans = 1,tmp = 1;
    h = v[0].fi,r = v[0].se;
    sort(all(v),[](const pll& a,const pll &b){
        if(a.fi == b.fi) return a.se < b.se;
        return a.fi < b.fi;});
    rep(i,n+m){
      if(v[i].fi > h && v[i].se > r){
        h = v[i].fi;
        r = v[i].se;
        ++ans;
      }
    }
    sort(all(v),[](const pll& a,const pll &b){
        if(a.se == b.se) return a.fi < b.fi;
        return a.se < b.se;});
    h = v[0].fi,r = v[0].se;
    rep(i,n+m){
      if(v[i].fi > h && v[i].se > r){
        h = v[i].fi;
        r = v[i].se;
        ++tmp;
      }
    }
    ans = max(ans,tmp);
    cout << ans << endl;
  }
  return 0;
}