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>
#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, int s2){
  if(pos == n+m) return 0;

  int a = 0, b = 0;

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

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

  return max(a, b);
}

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,-1));

  }
  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;
}

struct data{
  ll h,r,s;
  bool operator<(const data& d){
    return s < d.s;
  }
};

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n && n){
    vector<data> v;
    ll h,r,mh = 0;
    rep(i,n){
      cin >> h >> r;
      mh = max(mh,h);
      v.push_back({h,r,h+r});
    }
    cin >> m;
    rep(i,m){
      cin >> h >> r;
      mh = max(mh,h);
      v.push_back({h,r,h+r});
    }
    sort(all(v));
    ll ans = 0,c = 0;
    vvl(dp,1002,1002,0);
    REP(i,1,1001){
      REP(j,1,1001){
        if(c >= n+m) break;
        if(v[c].h <= i && v[c].r <= j){
          dp[i][j] = max(dp[i][j],dp[i-1][j-1]+1);
          ++c;
        }
        else{
          dp[i][j] = max(dp[i][j],max(dp[i][j-1],max(dp[i-1][j-1],dp[i-1][j])));
        }
      }
    }
//     rep(i,30){
//       scout(3) << i << ' ';
//     }
//     cout << endl;
//     rep(i,30){
//       scout(3) << i << ' ';
//       rep(j,30){
//         scout(3) << dp[i][j] << ' ';
//       }
//       cout << endl;
//     }
    rep(i,1002){
      ans = max(ans,dp[mh][i]);
    }
    cout << ans << endl;
  }
  return 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;
}