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 <cstdio>
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    for(i = 1 ; i <= m ; i++ ) {
        for(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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        printf("%d\n", lcs(x,y));
    }

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const 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;    // X[0]に空白を挿入
    Y = ' ' + Y;    // Y[0]に空白を挿入
    for (int i = 0; i <= m; i++) c[i][0] = 0;
    for (int j = 0; 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;
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
map <char,vector<int>> alx,aly;//x,y
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//yn文字目以降でX[xn]と同じやつを探す
    if(memo[xn][yn]!=-1)return memo[xn][yn];
    int ans=0;
	/*
    for(int i=xn; i<x.size();i++){
        for(int j=yn; j<y.size(); j++){
            if(alx[x[i]]==aly[y[j]]){
                ans=max(ans,lcs(i+1,j+1)+1);
            }
        }
    }
	*/
	for(int i=xn; i<x.size();i++){
        for(int j=0; j<aly[x[i]].size(); j++){
			if(aly[x[i]][j] < yn) continue;
			ans=max(ans,lcs(i+1,aly[x[i]][j]+1)+1);
			break;
		}
    }
    return memo[xn][yn]=ans;
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++){
				memo[i][j]=-1;
			}
        }

		alx.clear();
		aly.clear();

		for(int i=0; i<x.size(); i++) alx[x[i]].push_back(i);
		for(int i=0; i<y.size(); i++) aly[y[i]].push_back(i);
		

        //memset(memo, -1, sizeof(memo));
         
        cout << lcs(0,0) << endl;
 
    }
    return 0;
}



/*
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//Y[yn]とX[xn]を比較
    if(memo[xn][yn]!=-1)return memo[xn][yn];
	else if(xn==0 || yn==0)return 0;
	else if(x[xn]==y[yn])return memo[xn][yn] = lcs(xn-1,yn-1)+1;
	else  return memo[xn][yn] = max(lcs(xn,yn-1),lcs(xn-1,yn));
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++)memo[i][j]=-1;
        }
        //memset(memo, -1, sizeof(memo));
         
        cout << max(lcs(x.size(),y.size()),0) << endl;
 
    }
    return 0;
}*/

/*

#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n,memo[50]={};

int fib(int n){
	if(memo[n]!=0)return memo[n];
	return memo[n]=fib(n-1)+fib(n-2);
}


int main(){

	cin >> n;
	memo[0]=1;
	memo[1]=1;
	for(int i=2; i<=n; i++)
		memo[i]=memo[i-1]+memo[i-2];
	cout << memo[n] << endl;
	return 0;
}
*/

/*
#include<queue>
using namespace std;
int n,A[2000],q;//A:0origin
int memo[2000][2000];

bool solve(int i,int m){//i番目以降のAを使ってmを作る
	if(m==0) return true;//０はどんなときも作れる
	if(i==n) return false;//すべて使ってもつくれない
	//すでにやってるときメモを返す
	if(memo[i][m]!=0){
		if(memo[i][m]==-1)return false;
		else return true;
	}
	//solve(i,m)  A[i]を使う solve(i+1,m-A[i]) || 使わない　solve(i+1,m)
	bool X=false;
	if(m-A[i]>=0 && solve(i+1,m-A[i])) X=true;
	else if(solve(i+1,m)) X = true;

	if(X)memo[i][m]=1;
	else memo[i][m]=-1;

	 return X;

}

int main(){

	cin >> n;

	for(int i=0; i<n; i++) cin >> A[i];

	
	cin >> q;

	for(int i=0; i<q; i++){
		int m;
		cin >> m;
		for(int k=0; k<n; k++){
			for(int j=0; j<n; j++) memo[k][j]=0;
		}

		if(solve(0,m))cout << "yes" << endl;
		else cout << "no" << endl; 
	}

	return 0;
}

*/

/*
	bool dp[2000][2000];
	bool memo[2000][2000];//[順番][m-これまでの総和]
	
	bool solve(int now,int least){
		if(now == n) return false;//now>nならfalse
		else if(least == 0)return true;//least==0 >> true
		else if(memo[now][least])return dp[now][least];//already diside >> そのまま出来上がってるのを返す
		else if(solve(now+1,least))dp[now][least] = true;//A[now]をとらない >> solve(now++,least)
		else if(least-A[now] > 0 && solve(now++,least-A[now]))dp[now][least] = true;//A[now]をとる (least-A[now]>0 only)>> solve(now++,least-A[now])
		else dp[now][least] = false;
		memo[now][least] = true;
		return dp[now][least];
	}
	*/

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); (i)--)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
using namespace std;

string x, y;
int n, m;
int dp[1010][1010];

int solve(int i, int j){
    if(i==0 || j==0) return dp[i][j] = 0;
    if(dp[i][j] >= 0) return dp[i][j];
    if(x[i-1] == y[j-1]){
        dp[i][j] = max(solve(i-1, j), solve(i, j-1));
        return dp[i][j] = max(dp[i][j], solve(i-1, j-1)+1);
    }
    return dp[i][j] = max(solve(i-1, j), solve(i, j-1));
}

int main(){
    int q; cin >> q;
    rep(i, q){
        cin >> x >> y;
        n = (int)(x.size()); m = (int)(y.size());
        rep(i, n+1)rep(j, m+1) dp[i][j] = -1;
        cout << solve(n, m) << endl;
    }
}