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++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;
int X_length, Y_length;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	bool alreadySeenTable[256];
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	
	for (i = 0; i < 256; i++) {
		alreadySeenTable[i] = false;
	}
	i = x_begin;
	to = Y_length-1;
	while(i <= X_length-1) {
		j = y_begin;
		if (alreadySeenTable[X[i]] == true) {
			i++;
			continue;
		}
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			alreadySeenTable[X[i]] = true;
			i++;
		}
		else {
			alreadySeenTable[X[i]] = true;
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int lcs2(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}

	i = x_begin;
	to = Y_length - 1;
	while (i <= X_length - 1) {
		j = y_begin;
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs2(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			i++;
		}
		else {
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;

	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		X_length = stringX.size();
		Y = stringY.c_str();
		Y_length = stringY.size();
		printf("%d\n", lcs2(0, 0));
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <fstream>
#include <numeric>


using namespace std;

#define max(a,b) a > b ? a : b
#define min(a,b) a < b ? a : b

const int INF = 1 << 29;        // 十分大きい値にする

// 入力
string S, T;

// DP テーブル
int dp[1010][1010];
int n;
int ans[150];

int main() {

    cin >> n;

    for (int i = 0; i < n; i++)
    {
        cin >> S >> T;
        memset(dp, 0, sizeof(dp));    // 初期化
        for (int i = 0; i < S.size(); ++i) {
            for (int j = 0; j < T.size(); ++j) {
                if (S[i] == T[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i + 1][j]);
                dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j + 1]);
            }
        }

        ans[i] = dp[S.size()][T.size()];

    }

    for (int i = 0; i < n; i++)
    {
        cout << ans[i] << endl;
    }

}

C++: #include <bits/stdc++.h>
using namespace std;

int lcs(const string& X,
    const string& Y) 
{
    int lenX = X.length();
    int lenY = Y.length();
    vector<vector<int>> dp(lenX + 1, vector<int>(lenY + 1));
    for (int i = 1; i <= lenX; ++i) {
        for (int j = 1; j <= lenY; ++j) {
            if (X[i-1] == Y[j-1]) {
                dp[i][j] = dp[i-1][j-1] + 1;
            }
            else {
                dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
            }
        }
    }
    return dp[lenX][lenY];
}

int main() {
    int q;
    cin >> q;
    vector<string> Xs(q);
    vector<string> Ys(q);
    for (int i = 0; i < q; ++i) {
        cin >> Xs[i];
        cin >> Ys[i];
    }
    for (int i = 0; i < q; ++i) {
        cout << lcs(Xs[i], Ys[i]) << endl;
    }



}

C++: // #include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
#include <numeric>
#include <set>
#include <queue>
#include <cstdio>
using namespace std;
#define rep(i, n) for(int i=0;i<n;i++)
#define irep(i, n) for(int i=n-1;i>=0;i--)
#define lrep(i, n) for(long long i=0;i<n;i++)
typedef long long ll;
typedef vector<long long> llv;
typedef vector<vector<long long> > llvv;

int N = 1000;

int lcs(string X, string Y){
    int c[N+1][N+1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (int i=1;i<=m;i++) c[i][0] = 0;
    for (int j=1;j<=n;j++) c[0][j] = 0;
    for (int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if (X[i] == Y[j]){
                c[i][j] = c[i-1][j-1] + 1;
            }else{
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main(){
    string s1, s2;
    int n; cin >> n;
    rep(i,n){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>

using namespace std;

int lcslen[1001][1001]={0};
int compute_lcslen(const string &x,const string &y);

int main(){
  int q;
  cin >> q;
  for(int i=0;i<q;++i){
    string x,y;
    cin >> x;
    cin >> y;
    cout << compute_lcslen(x,y) << endl;
  }

  return 0;
}

int compute_lcslen(const string &x,const string &y){
  for(int i=1;i<=x.length();++i){
    for(int j=1;j<=y.length();++j){
      if(x[i-1]==y[j-1]){
        lcslen[i][j] = lcslen[i-1][j-1]+1;
      }else{
        lcslen[i][j] = max(lcslen[i-1][j],lcslen[i][j-1]);
      }
    }
  }
  return lcslen[x.length()][y.length()];
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[111][111];
int lcs(string x,string y){
	for(int i=0;i<12321;i++)dp[i/111][i%111]=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			if(i==0 || j==0){dp[i][j]=0;}
			else if(x[j]==y[i]){dp[i][j]=dp[i-1][j-1]+1;}
			else{dp[i][j]=max(dp[i][j-1],dp[i-1][j]);}
		}
	}
	int MAXN=0;
	for(int i=0;i<=y.size();i++){
		for(int j=0;j<=x.size();j++){
			MAXN=max(MAXN,dp[i][j]);
		}
	}
	return MAXN;
}
int main(){
	int n;string S,T;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>S>>T;cout<<lcs(S,T)<<endl;
	}
	return 0;
}