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 <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = unsigned;
using u64 = unsigned long long;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template<class T> void chmin(T &a, const T &b){ a = (a < b ? a : b); }
template<class T> void chmax(T &a, const T &b){ a = (a > b ? a : b); }

int main() {
    int n;
    while(cin >> n, n){
        vector<pair<int, int>> v(n);
        for (int i = 0; i < n; ++i) {
            scanf("%d %d", &v[i].first, &v[i].second);
        }
        int m;
        cin >> m;
        v.resize(n+m);
        for (int i = 0; i < m; ++i) {
            scanf("%d %d", &v[n+i].first, &v[n+i].second);
        }
        sort(v.begin(),v.end());
        vector<int> dp(n+m, 1);
        for (int i = 0; i < n+m; ++i) {
            for (int j = 0; j < i; ++j) {
                if(v[j].first < v[i].first && v[j].second < v[i].second){
                    chmax(dp[i], dp[j]+1);
                }
            }
        }
        cout << *max_element(dp.begin(),dp.end()) << "\n";
    }
    return 0;
}

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

using namespace std;

bool cmp(pair<int,int> a,pair<int,int> b){
  if(a.first!=b.first){
    return a.first<b.first;
  }else{
    return a.second>b.second;
  }
}

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(),cmp);
    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 <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define MAX_NM 202

struct M{
  int r, h;
};

bool ascending_M_r(const M& l, const M& r){
  return l.r < r.r;
}
bool ascending_M_h(const M& l, const M& r){
  return l.h < r.h;
}

int main(void){
  int n, m, dp[MAX_NM];
  M a[MAX_NM];

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

    int lis_r = 0, lis_h = 0;
    sort(a, a + n + m, ascending_M_r);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h < a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_r = dp[n+m-1];

    sort(a, a + n + m, ascending_M_h);

    dp[0] = 1;
    for(int i = 1; i < n + m; i++){
      dp[i] = 1;
      for(int j = 0; j < i; j++){
        if(a[j].r < a[i].r && a[j].h < a[i].h){
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    lis_h = dp[n+m-1];

    printf("%d\n", max(lis_h, lis_r));
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>
#include<numeric>
#include<cassert>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define rep(i,s,n) for(int i=(s); 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 IN(x,s,g) ((x) >= (s) && (x) < (g))
#define ISIN(x,y,w,h) (IN((x),0,(w)) && IN((y),0,(h)))
#define print(x) printf("%d\n",x)

using namespace std;

typedef unsigned int uint;
typedef long long ll;

const int _dx[] = {0,1,0,-1};
const int _dy[] = {-1,0,1,0};

int getInt(){
  int ret = 0,c;
  c = getchar();
  while(!isdigit(c)) c = getchar();
  while(isdigit(c)){
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}

int n;
int m;
pair<int,int> h[200];

int dp[2][200];

int solve(int pos, int size){
  if(pos == n+m) return 0;

  if(dp[0][pos] == -1)
    dp[0][pos] = solve(pos+1, size);

  if(size < h[pos].s){
    if(dp[1][pos] == -1)
      dp[1][pos] = solve(pos+1, h[pos].s) + 1;
  }else
    dp[1][pos] = 0;

  return max(dp[0][pos], dp[1][pos]);
}

int main(){
  while(n = getInt()){
    REP(i,n){
      h[i].f = getInt();
      h[i].s = getInt();
    }

    m = getInt();
    REP(i,m){
      h[i+n].f = getInt();
      h[i+n].s = getInt();
    }

    sort(h, h+n+m);

    memset(dp, -1, sizeof(dp));

    print(solve(0,-1));

  }
  return 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];
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 1;
  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)-1<<endl;
  }
}