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++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

template<typename Monoid> class SegTree {
  using T = typename Monoid::value_type;

  Monoid m;
  std::vector<T> tree; // 1-indexed
  int size = 1;

  void propTo(int i) { tree[i] = m(tree[i * 2], tree[i * 2 + 1]); }

public:
  SegTree(const int n = 0) {
    while (size < n) size *= 2;
    tree.assign(size * 2, m.id());
  }

  template<typename InputIterator> SegTree(InputIterator first, InputIterator last) {
    int n = std::distance(first, last);
    while (size < n) size *= 2;
    tree.resize(size * 2, m.id());
    std::copy(first, last, tree.begin() + size);
    for (int i = size - 1; i >= 1; i--) propTo(i);
  }

  T fold(int l, int r) { // [l, r)
    T accl = m.id(), accr = m.id();
    for (l += size, r += size; l < r; l /= 2, r /= 2) {
      if (l & 1) accl = m(accl, tree[l++]);
      if (r & 1) accr = m(tree[--r], accr);
    }
    return m(accl, accr);
  }

  void update(int i, const T &x) {
    tree[i += size] = x;
    while (i /= 2) propTo(i);
  }

  const T &operator[](int i) const { return tree[i + size]; }
};

template<typename T, T lowerInf = std::numeric_limits<T>::min()> struct maxMonoid {
  using value_type = T;
  constexpr value_type id() { return lowerInf; }
  constexpr value_type operator()(const value_type &a, const value_type &b) { return a < b ? b : a; }
};

main {
  int n;
  while (cin >> n, n) {
    vpii v(n);
    cin >> v;
    int m;
    cin >> m;
    {
      vpii w(m);
      cin >> w;
      v.reserve(n + m);
      for (auto &x : w) v.push_back(move(x));
    }
    sort(all(v), [](auto &a, auto &b) {
      if (a.first != b.first) return a.first < b.first;
      return a.second > b.second;
    });
    SegTree<maxMonoid<int, 0>> t(1000);
    for (auto &p : v) {
      int &r = p.second;
      t.update(r, t.fold(0, r) + 1);
    }
    cout << t.fold(0, 1000) << endl;
  }
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;
typedef pair<int, int>P;

P v1[200], v2[200];
int main() {
	int n;
	while (scanf("%d", &n), n) {
		rep(i, n) {
			int h, r; scanf("%d%d", &h, &r);
			v1[i] = P(h, r);
		}
		int m; scanf("%d", &m);
		rep(i, m) {
			int h, r; scanf("%d%d", &h, &r);
			v2[i] = P(h, r);
		}
		sort(v1, v1 + n);
		sort(v2, v2 + m);
		v1[n] = v2[n] = P(2000, 2000);
		int cnt1 = 0;
		rep(i, n + 1) {
			rep(j, m) {
				if ((!i||(v1[i - 1].first < v2[j].first&&v1[i - 1].second < v2[j].second)) &&
					v2[j].first < v1[i].first&&v2[j].second < v1[i].second)
					cnt1++;
			}
		}
		int cnt2 = 0;
		rep(i, m + 1) {
			rep(j, n) {
				if ((!i||(v2[i - 1].first < v1[j].first&&v2[i - 1].second < v2[j].second)) &&
					v1[j].first < v2[i].first&&v1[j].second < v2[i].second)
					cnt2++;
			}
		}
		printf("%d\n", max(cnt1 + n, cnt2 + m));
	}
}

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

int main() {
    int n, m, h, r;
    while (cin >> n, n) {
        vector<pair<int,int> > v;
        for (int i=0; i<n; ++i) {
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        cin >> m;
        for (int i=0; i<m; ++i) {
            cin >> h >> r;
            v.push_back(make_pair(h, r));
        }
        sort(v.begin(), v.end());
        int mx = 0, dp[200];
        for (int i=0; i<n+m; ++i) {
            dp[i] = 1;
            for (int j=0; j<i; ++j) {
                if (v[j].first < v[i].first && v[j].second < v[i].second) {
                    dp[i] = max(dp[i], dp[j] + 1);
                }
            }
            mx = max(mx, dp[i]);
        }
        cout << mx << endl;
    }
    return 0;
}

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

const int SIZE = 200;

struct Doll
{
	int h;
	int r;
	bool operator<( const Doll& d ) const
	{
		if( this->h < d.h ) return true;
		if( this->h > d.h ) return false;
		if( this->r < d.r ) return true;
		return false;
	}
};

int main()
{

	int n, m;
	while( cin >> n && n ){
		int memo[ SIZE ][ SIZE ];
		Doll doll[ SIZE ];
		memset( memo, 0, sizeof(memo) );
		for( int i = 0; i < n; ++i ){
			cin >> doll[ i ].h >> doll[ i ].r;
		}
		cin >> m;
		for( int i = 0; i < m; ++i ){
			cin >> doll[ n + i ].h >> doll[ n + i ].r;
		}
		int numDolls = n + m;
		sort( doll, doll + numDolls );

		int result = 0;
		for( int k = 0; k < numDolls; ++k ){
			for( int i = 0; i < numDolls; ++i ){
				for( int j = 0; j < numDolls; ++j ){
					if( doll[ i ].h < doll[ j ].h && doll[ i ].r < doll[ j ].r ){
						memo[ i ][ j ] = max( memo[ i ][ j ], max( 2, memo[ i ][ k ] + memo[ k ][ j ] - 1 ) );
						if( memo[ i ][ j ] > result ){
							result = memo[ i ][ j ];
						}
					}
				}
			}
		}
		cout << result << endl;
	}

	return 0;
}