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 <bits/stdc++.h>
#include <iostream>
#include <map>
#include <cmath>
#include <ios>
#include <iomanip>
#include <algorithm>
#include <queue>
#include <stack>
#include <numeric>
//#include <windows.h>
using namespace std;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = V<VV<T>>;
template<class T> using G = greater<T>;
template<class T1, class T2> using P = pair<T1, T2>;
using I = int;
using D = double;
using B = bool;
using C = char;
using S = string;
using LL = long long;
using LD = long double;
using ULL = unsigned long long;
using PII = P<I, I>;
using VPII = V<PII>;
using PLL = P<LL, LL>;
using VPLL = V<PLL>;
using VI = V<I>;
using VVI = VV<I>;
using VLL = V<LL>;
using VVLL = VV<LL>;
using VC = V<C>;
using VVC = VV<C>;
using VS = V<S>;
using VVS = VV<S>;
using VB = V<B>;
using VVB = VV<B>;
#define REP(type, i, n) for (type i = 0; i < (type)(n); ++i)
#define REP2(type, i, m, n) for (type i = (m); i < (type)(n); ++i)
#define REPR(type, i, n) for (type i = (n)-1; i >= 0; --i)
#define REPR2(type, i, m, n) for (type i = (n)-1; i >= (m); --i)
#define REPx(x, a) for(auto x : a)
#define ALL(a) a.begin(), a.end()
#define SORT(a) sort(ALL(a))
#define SORTR(a, type) sort(ALL(a), G<type>())
#define REVERSE(a) reverse(ALL(a))
#define SIZE(a, type) ((type)(a).size())
#define bit_search(bit, n) REP(LL, bit, 1LL<<(n))
#define bit_check(bit, i) ((bit>>(i)) & 1)
#define setpre(n) fixed << setprecision((n))
#define UNIQUE(a) do {SORT(a); (a).erase(unique(ALL(a)), (a).end());} while(0)
#define MAX(a) *max_element(ALL(a))
#define MIN(a) *min_element(ALL(a))
#define bisect_left(a, x) lower_bound(ALL(a), (x)) - a.begin()
#define bisect_right(a, x) upper_bound(ALL(a), (x)) - a.begin()
#define INPUT(a) REPx(&x, a) cin >> x;
#define INPUTP(a) REPx(&x, a) cin >> x.first >> x.second;
#define OUTPUT_PERMUTATION(n) do{VI v(n); iota(ALL(v), 1); do{REPx(x, v) cout << x << " "; ENDL} while(next_permutation(ALL(v)));} while(0);
#define MAKE_PERMUTATION(n, PER) do{VVI a(fact(n), VI(n)); int idx = 0; VI v(n); iota(ALL(v), 1); do{REP(roop, n) a[idx][roop] = v[roop]; idx++;} while(next_permutation(ALL(v))); PER = a;} while(0); // int fact(), VVI PERを宣言しておく、n=10が限界(500 ms)
#define ENDL cout << endl;

const int INF = 2e9;
const LL MOD = 1e9 + 7;

template<class T> using PRIORITY_QUEUE = priority_queue< T, V<T>, G<T> >;
template<class T> inline bool chmin(T &a, T b){if (a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T &a, T b){if (a < b) {a = b; return true;} return false;}
template<class T> inline void debug1(V<T> A){REP(int, i, SIZE(A, int)){if (A[i] == INF) cout << "I "; else cout << A[i] << " ";}}
template<class T> inline void debug2(VV<T> A){REP(int, i, SIZE(A, int)){REP(int, j, SIZE(A[0], int)){if (A[i][j] == INF) cout << "I "; else cout << A[i][j] << " ";}ENDL}}

int LCS(auto A, auto B)
{
  // 配列 A、B のLCS(Longest Common Subsequence)を求める
  int N = SIZE(A, int), M = SIZE(B, int);
  VVI dp(N+1, VI(M+1, 0));
  REP(int, i, N) REP(int, j, M)
  {
    dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
    if (A[i] == B[j]) dp[i+1][j+1] = dp[i][j] + 1;
  }
  return dp[N][M];
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  int q;
  cin >> q;
  while(q--)
  {
    string x, y;
    cin >> x >> y;
    cout << LCS(x, y) << endl;
  }

  return 0;
}

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

int LCS(string x,string y){
	int xs,ys;
	xs=x.size();ys=y.size();
	int dp[xs+1][ys+1];
	for(int i=0;i<xs+1;i++)
		for(int j=0;j<ys+1;++j)
			dp[i][j]=0;
	for(int i=0;i<xs+1;i++){
		for(int j=0;j<ys+1;++j){
			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(){
	int n;
	cin >> n;
	string s1,s2;
	for(int i=0;i<n;i++){
		cin >> s1 >> s2;
		cout << LCS(s1,s2) << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int n, xl, yl, dp[1003][1003]; char x[1003], y[1003];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%s", x); xl = strlen(x);
		scanf("%s", y); yl = strlen(y);
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= xl; i++) {
			for(int j = 1; j <= yl; j++) {
				if(x[i - 1] == y[i - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
				else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		printf("%d\n", dp[xl][yl]);
	}
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>

typedef long long ll;

using namespace std;

char a[1010],b[1010];
int maxlen[1010][1010];

int main() {
	ios::sync_with_stdio(false);
	
	
	while (cin>>a>>b){
		int len1 = strlen(a);
		int len2 = strlen(b);
		memset(maxlen,0,sizeof(maxlen));
		
		for(int i = 1 ; i<=len1 ; i++){
			for(int j = 1; j<=len2 ; j++){
				if(a[i-1] == b[j-1]){
					maxlen[i][j] = maxlen[i-1][j-1] + 1;
				}
				else{
					maxlen[i][j] = max( maxlen[i-1][j] , maxlen[i][j-1]);
				}
			}
		}
		cout<<maxlen[len1][len2]<<endl;
	} 
	return 0;
}

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

int lcs(string X, string Y) {
int max = 0;
int n = X.size();
int m = Y.size();
int dp[1000+1][1000+1];

X = ' '+X;
Y = ' '+Y;

for(int i = 1:i <= n; i++)
dp[i][0] = 0;

for(int i = 1; i <= m; i++)
dp[0][i] = 0;

for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(X[i] == Y[j])
dp[i][j] = dp[i-1][j-1]+1;
else if(dp[i-1][j] > dp[i][j-1])
dp[i][j] = dp[i-1][j];
else
dp[i][j] = dp[i][j-1];

if(max < dp[i][j])
max = dp[i][j];
}
}

return max;
}

int main() {
int N;
cin >> N;

string X, Y;
for (int i = 0; i < N; i++) {
cin >> X >> Y;

cout << lcs(X, Y) << endl;
}
return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n - 1; i >= 0; i--)

const int inf = 1000000007;
const int MOD = 1000000007;
const long long INF = 1000000000000000007;

int q;
int dp[1010][1010];

int solve(string x, string y)
{
    int n = x.length();
    int m = y.length();
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (x[i] == y[j])
            {
                dp[i + 1][j + 1] = dp[i][j] + 1;
            }
            else
            {
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[n][m];
}

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