Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

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

#define REP(i, n) for(int (i) = 0; (i) < (n); (i)++)

string str1, str2;
int dp[1001][1001];
int k;

int solve(int i, int j)
{
  if(dp[i][j] > 0)
    return dp[i][j];
  if(i >= str1.size() || j >= str2.size()){
    if(i < str1.size()){
      return solve(i + 1, k);
    }else{
      return 0;
    }
  }
  if(str1[i] == str2[j]){
    k = j + 1;
    int n, m = 0;
    for(int l = i + 1; l < str1.size(); l++){
      n = solve(l, j + 1);
      m = (n > m) ? n : m;
    }
    dp[i][j] = m + 1;
    return dp[i][j];
  }else{
    return solve(i, j + 1);
  }
}
  
int main()
{
  int q, n, ans = 0;
  cin >> q;
  cin.ignore();
  REP(i, q){
    getline(cin, str1);
    getline(cin, str2);
    if(str1.size() < str2.size())
      swap(str1, str2);
    fill(&dp[0][0], &dp[1001][0], -1);
    for(int j = str1.size() - 1; j >= 0; j--){
      n = solve(j, 0);
      ans = (n > ans) ? n : ans;
    }
    cout << ans << endl;
    ans = 0;
  }
}

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

int main(){
  
  int q, lcs[1010][1010] = {};
  string a, b;

  //input
  cin >> q;
  for(int i = 0;i < q;i++){
    cin >> a;
    cin >> b;

    for(int r = 0;r < (int)a.size();r++){
      for(int c = 0;c < (int)b.size();c++){
	//if a_i = b_j, chain a_i to LCS of a_{i-1} and b_{j-1}
        if(a[r] == b[c]) lcs[r + 1][c + 1] = lcs[r][c] + 1;
	//else LCS of a_{i-1} and b_j or a_i and b_{j-1}
        else lcs[r + 1][c + 1] = max(lcs[r + 1][c], lcs[r][c + 1]);
      }
    }
    
    //output
    cout << lcs[a.size()][b.size()] << endl;
  }
  
  return 0;
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>

#define debug(x) cout << #x << "==" << x << endl;

using namespace std;

//??\???
#define MAX_LEN 1000
int N;
int dp[MAX_LEN + 1][MAX_LEN + 1];

void print_dp_table(int n, int m) {
  for(int i = 0; i < n; i++) {
    for(int j = 0; j < m; j++) {
      cout <<setw(2) << dp[i][j];
    }
    cout << "\n";
  }
  cout << "\n";
}

int main() {
  ios::sync_with_stdio(false);

  cin >> N;

  string s1, s2;

  while( cin >> s1 >> s2 ) {

    std::vector<char> a;
    std::vector<char> b;
    for(char& c : s1) {
      a.push_back(c);
    }
    for(char& c : s2) {
      b.push_back(c);
    }


    // dp ????????????????????????
    fill(dp[0], dp[MAX_LEN + 1], 0);

    for( int i = 0; i < (int)a.size(); i++) {
      for( int j = 0; j < (int)b.size(); j++) {
        if( a[i] == b[j] ) {
          dp[i + 1][j + 1] = dp[i][j] + 1;
        } else {
          dp[i + 1][j + 1] = max( dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }

    // print_dp_table(a.size(), b.size());
    cout << dp[a.size()][b.size()] << endl;

  }

  return 0;
}

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

int dp[1005][1005];
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int lcs(string &x, string &y) {
    memset(dp, 0, sizeof(dp));
    int xn = x.size();
    int yn = y.size();
    rep(i, xn) {
        rep(j, yn) {
            if (x[i] == y[j])
                dp[i + 1][j + 1] = max(dp[i][j] + 1, max(dp[i][j + 1], dp[i + 1][j]));
            else
                dp[i + 1][j + 1] = max(dp[i][j], max(dp[i][j + 1], dp[i + 1][j]));
        }
    }
    return dp[xn][yn];
}

int main() {
    int n;
    cin >> n;
    rep(i, n) {
        string x, y;
        cin >> x >> y;
        int ans = lcs(x, y);
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <set>
#include <map>
#include <functional>
#include <string>
#include <cmath>
using namespace std;
typedef long long ll;
#define P pair<ll,ll>
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //ai>=v  x is sorted
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //ai>v  x is sorted
#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted
#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9
#define REV(x) (reverse(x.begin(),x.end())) //reverse
#define TO(x,t,f) ((x)?(t):(f))
#define CLR(mat) memset(mat, 0, sizeof(mat))
#define FILV(x,a) fill(x.begin(),x.end(),a)
#define FILA(ar,N,a) fill(ar,ar+N,a)
#define NEXTP(x) next_permutation(x.begin(),x.end())
ll gcd(ll a,ll b){if(a<b)swap(a,b);if(a%b==0)return b;else return gcd(b,a%b);}
ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);}//saisyo kobaisu
#define pb push_back
#define pri(aa) cout<<(aa)<<endl
#define mp(x,y) make_pair(x,y)
#define fi first
#define se second
const ll INF=1e9+7;
const ll N = 1007;


ll LCS(string x,string y){
	ll xs,ys;
	xs=x.size();ys=y.size();
	ll dp[xs+1][ys+1];
	FOR(i,0,xs+1)FOR(j,0,ys+1)dp[i][j]=0;
	FOR(i,0,xs+1){
		FOR(j,0,ys+1){
			if(i==0||j==0)dp[i][j]=0;
			else{
				if(x[i-1]==y[j-1])dp[i][j]=dp[i-1][j-1]+1;
				else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
			}
		}
	}
	return dp[xs][ys];
}



int main(){
	ll n;
	cin>>n;
	FOR(i,0,n){
		string x,y;
		cin>>x>>y;
		cout<<LCS(x,y)<<endl;
	}
}

C++: #include <vector>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
int main(){
	int n;
	cin >> n;
	while(n--){
		string a,b;
		cin >> a >> b;
		int asize = a.size(),bsize = b.size();
		vector<vector<int> > dp(asize+1,vector<int>(bsize+1,0));
		for(int i=1;i<asize+1;i++){
			bool is_match = false;
			for(int j=1;j<bsize+1;j++){
				if(!is_match){
					if(a[i-1] == b[j-1]){
						dp[i][j] = dp[i-1][j] + 1;
						is_match = true;
					}else{
						dp[i][j] = dp[i-1][j];
					}
				}else{
					dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				}
			}
		}
		/*		for(int i = 0;i<asize+1;i++){
		copy(dp[i].cbegin(),dp[i].cend(),ostream_iterator<int>(cout, " ") );
		cout << endl;
		}*/
				
		cout << dp[asize][bsize] << endl;
	}
}