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 <vector>
using namespace std;


class doll;


int n, m;
vector<doll> d;
vector<int> dp = { 0 };


class doll{
public:
	int h;
	int r;
	doll(int h, int r) : h(h), r(r){}
	int operator[](char member){
		if (member == 'h') return h;
		else if(member == 'r') return r;
		else return -1;
	}
};


int solve(char member){
	dp[0] = d[0][member];
	for (int i = 1; i < d.size(); i++){
		if (dp.back() < d[i][member]){
			dp.push_back(d[i][member]);
		}
		else{
			*lower_bound(dp.begin(), dp.end(), d[i][member]) = d[i][member];
		}
	}
	return dp.size();
}


template<typename T>
void clear(vector<T>& v){
	vector<T> emp;
	swap(v, emp);
}


int main(){
	while (cin >> n, n != 0){
		for (int i = 0; i < n; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}
		cin >> m;
		if (m == 0) return 0;
		for (int i = 0; i < m; i++){
			int h, r;
			cin >> h >> r;
			d.push_back({ h, r });
		}

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.h < d2.h);
		});

		int ans = solve('r');

		clear(dp);
		dp.push_back(0);

		sort(d.begin(), d.end(), [](doll& d1, doll& d2){
			return (d1.r < d2.r);
		});

		ans = max(ans, solve('h'));

		cout << ans << endl;

		clear(d);
		clear(dp);
		dp.push_back(0);
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>

using namespace std;

typedef long long ll;
typedef pair<int,int> P;

static const double eps = 10e-8;

class mycmp{
public:
	bool operator()(const P p1,const P p2){
		if(p1.first == p2.first || p1.second == p2.second) return true;
		return false;
	}
};

int main(){
	int n;
	
	while(~scanf("%d",&n)){
		if(n==0) break;
		vector<P> dolls;

		for(int i=0;i<n;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}

		int m;
		scanf("%d",&m);
		for(int i=0;i<m;i++){
			int h,r;
			scanf("%d %d",&h,&r);
			dolls.push_back(P(h,r));
		}
		sort(dolls.begin(),dolls.end());
		vector<P>::iterator it = unique(dolls.begin(),dolls.end(),mycmp());
		dolls.erase(it,dolls.end());

		int ans=1;
		for(int i=0;i+1<dolls.size();i++){
			if(dolls[i].first < dolls[i+1].first
				&& dolls[i].second < dolls[i+1].second){
					ans++;
			}
		}

		cout << ans << endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

class Doll {
public:
	int h, r;

	Doll() {
		h = r = 0;
	}
};

Doll DollData[200];
int DpResult[200];


bool cmp(const Doll& d1, const Doll &d2) {
	return d1.h < d2.h;
}

int main() {
	int n, m, i;

	while (cin >> n, n) {
		for (i = 0; i < n; i++) {
			cin >> DollData[i].h >> DollData[i].r;
		}
		cin >> m;
		m += n;
		for (; i < m; i++) {
			cin >> DollData[i].h >> DollData[i].r;
		}
		sort(DollData, DollData + m, cmp);
		for (i = 0; i < m; i++) {
			DpResult[i] = 1;
		}

		for (int i = 0; i < m; i++) {
			for (int j = 0; j < m; j++) {
				if ( DollData[i].h>DollData[j].h && DollData[i].r>DollData[j].r ) {
					DpResult[i] = max(DpResult[i], DpResult[j] + 1);
				}
			}
		}
		sort(DpResult, DpResult + m);
		cout << DpResult[m - 1] << endl;
	}

	return 0;
}

C++: #include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
#define F first
#define S second
int main() {
	int n, m;
	while (scanf("%d",&n)) {
		if (n==0) break;
		P ich[n];
		for (int i=0; i<n; i++) scanf("%d%d",&ich[i].F,&ich[i].S);
		scanf("%d",&m);
		P ji[m];
		for (int i=0; i<m; i++) scanf("%d%d",&ji[i].F,&ji[i].S);
		P mat[n+m];
		for (int i=0; i<n; i++) mat[i]=ich[i];
		for (int i=0; i<m; i++) mat[i+n]=ji[i];
		sort(mat, mat+n+m);
		int dp[n+m], res=0;
		dp[0]=1;
		for (int i=1; i<n+m; i++) {
			dp[i]=1;
			for (int j=0; j<i; j++) {
				if (mat[j].F<mat[i].F&&mat[j].S<mat[i].S) dp[i]=max(dp[i],dp[j]+1);
			}
			res=max(res,dp[i]);
		}
		printf("%d\n",res);
	}
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <utility>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cctype>
using namespace std;
 
#define rep(i,n) for(int i=0; i<n; i++)
#define repa(i,s,e) for(int i=s; i<=e; i++)
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int n, m, h, r;
vector<pii> v;
int dp[201];

void solve() {
	sort(v.begin(), v.end());

	fill(dp, dp+201, 1);
	int l = v.size();

	rep(i,l) {
		repa(j,i+1,l-1) {
			if(v[i].second < v[j].second) {
				dp[j] = max(dp[j], dp[i]+1);
			}
		}
	}

	int maxval = 0;
	rep(i,l) {
		maxval = max(maxval, dp[i]);
	}
	cout << maxval << endl;
}

int main() {
	while(cin>>n,n) {
		v.clear();
		rep(i,n) {
			cin>>h>>r;
			v.push_back(pii(h,r));
		}
		cin>>m;
		rep(i,m) {
			cin>>h>>r;
			v.push_back(pii(h,r));
		}
		solve();
	}
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
#define f first
#define s second
#define pb(a,b) push_back(make_pair(a,b))
#define g(a,b) for(int i=a;i<b;i++)
int main(){
    int n;
    while(cin>>n,n){
        vector<pair<int,int> >v;
        int m,x,y,a=1,c=1,b=201;
        g(0,n){
            cin>>x>>y;
            v.pb(x, y);
        }
        cin>>m;
        g(0,m){
            cin>>x>>y;
            v.pb(x, y);
        }
        sort(v.begin(),v.end());
        x=v[0].f,y=v[0].s;
        g(1,m+n){
            if(v[i].s>y&&v[i].f>x){
                a++;
                x=v[i].f,y=v[i].s;
            }
            else
                b=min(b,i);
        }
        if(b<201){
            x=v[b].f,y=v[b].s;
            g(b+1,m+n){
                if(v[i].s>y&&v[i].f>x){
                    c++;
                    x=v[i].f,y=v[i].s;
                }
            }
        }
        cout<<max(a,c)<<endl;
    }
}