Go: package main

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

const max int = 1000 + 1
var A [][]int

func lcs(x, y string) int {
	x = " " + x
	y = " " + y
	for i:=0;i<len(x); i++ {
		for j:=0;j<len(y); j++ {
			A[i][j] = 0
		}
	}
	for i:=1; i<len(x); i++ {
		for j:=1; j<len(y); j++ {
			if x[i] == y[j] {
				A[i][j] = A[i-1][j-1] + 1
			} else if A[i-1][j] > A[i][j-1] {
				A[i][j] = A[i-1][j]
			} else {
				A[i][j] = A[i][j-1]
			}
		}
	}
	return A[len(x)-1][len(y)-1]
}

func nextInt(scanner *bufio.Scanner) int {
	scanner.Scan()
	num, _ := strconv.Atoi(scanner.Text())
	return num 
}

func nextStr(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}


func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	A = make([][]int, max)
	for i := range A {
		A[i] = make([]int, max)
	}
	n := nextInt(scanner)
	for i:=0; i<n; i++ {
		x := nextStr(scanner)
		y := nextStr(scanner)
		fmt.Printf("%d\n", lcs(x, y))
	}
}

C++: #include <iostream>
#include <algorithm>
#include <string>
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;
    Y = ' ' + Y;
    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<cstdio>
#include<cstring>
using namespace std;

int main(){
  int n;
  char x[10000],y[10000];
  int dp[10010][10010];
  //load n;
  cin >>n;
  for(int i=0;i<n;i++){
    //load string
    scanf("%s",x);
    scanf("%s",y);
    //dp
    for(int j=0;j<strlen(x);j++){
      for(int k=0;k<strlen(y);k++){
	if(x[j]==y[k]){
	  dp[j+1][k+1]=dp[j][k]+1;
	}

	else{
	  if(dp[j][k+1]>dp[j+1][k]){
	    dp[j+1][k+1]=dp[j][k+1];
	  }

	  else{
	    dp[j+1][k+1]=dp[j+1][k];
	  }


	}
      }
    }
    cout <<dp[strlen(x)][strlen(y)]<<endl;
  }



  return 0;
}

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

int dp[1001][1001];

int main() {
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string x, y;
    cin >> x >> y;
    int xlen = x.length(), ylen = y.length();
    for (int j = 0; j <= ylen; j++) dp[0][j] = 0;
    for (int i = 1; i <= xlen; i++) {
      dp[i][0] = 0;
      for (int j = 1; j <= ylen; j++) {
        dp[i][j] = x[i - 1] == y[j - 1] ? dp[i - 1][j - 1] + 1
                                        : max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
    cout << dp[xlen][ylen] << endl;
  }
}

C++: #include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;

int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int q;
    string s,t;
    cin>>q;
    vector<int> v;
    while(q--){
    	cin>>s>>t;
    	int n=s.length(),m=t.length();
    	int dp[n+1][m+1];
    	memset(dp,0,sizeof(dp));
    	for(int i=1;i<=n;i++){
    		for(int j=1;j<=m;j++){
    			dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    			if(s[i-1]==t[j-1])dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
    		}
    	}
    	v.pb(dp[n][m]);
    }
    for(auto& i:v){
    	cout << i<<endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <fstream>
#include <chrono>
#define lint long long int
#define FOR(x, to) for(int x=0; x<(int)(to); x++) // for簡略表記
#define DUMP(ar) for(int loop=0; loop<(int)ar.size(); loop++) cout << "[" << loop << "]:" << ar[loop] << endl // 配列一覧表示
#define DUMPL(ar) for(int loop=0; loop<(int)ar.size(); loop++) { cout << setw(2) << ar[loop]; if(loop<(int)ar.size()-1) cout << ' '; } cout << endl; // 配列一行一覧表示
#define COUT(x) cout << x << endl // 単要素出力
#define ALL(ar) ar.begin(), ar.end() // コンテナの全要素
#define LOOPD(i) cout << "ループ[" << i << "]回目---------------" << endl // ループ回数デバグ
using namespace std;
// cout << fixed << setprecision(4) で小数点以下4桁の桁そろえ
// cout << setw(5) << ... で表示形式を5桁に設定 5桁未満なら頭に空白がつく 5桁越なら先頭から5桁まで
// cout << setw(5) << setfill('0') << ... で空白部分の埋め文字に5を使う
// getline(cin, [変数]); で空白など(ホワイトスペース)を含む入力
// cin >> ws; でそこまでに入力ストリームに残されたホワイトスペースを取り除く(=以降のgetlineの有効化)

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    int q;
    cin >> q;
    vector<string> s1(q), s2(q);
    for(int i=0; i<q; i++){
        cin >> s1[i] >> s2[i];
    }

    vector<int> ans(q);
    for(int i=0; i<q; i++){
        int s1_size = s1[i].size();
        int s2_size = s2[i].size();
        
        vector<vector<int>> dp(s1_size, vector<int>(s2_size, 0));
        // 初期化
        bool flag_same = false;
        for(int j=0; j<s1_size; j++){
            if(s1[i][j] == s2[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[j][0] = 1;
            else dp[j][0] = 0;
        }
        flag_same = false;
        for(int j=0; j<s2_size; j++){
            if(s2[i][j] == s1[i][0] && !flag_same){
                flag_same = true;
            }
            if(flag_same) dp[0][j] = 1;
            else dp[0][j] = 0;
        }

        // 計算
        for(int j=1; j<s1_size; j++){
            for(int k=1; k<s2_size; k++){
                if(s1[i][j] == s2[i][k]){
                    dp[j][k] = min(dp[j-1][k-1], min(dp[j-1][k], dp[j][k-1])) + 1;
                }else{
                    dp[j][k] = max(dp[j-1][k], dp[j][k-1]);
                }
            }
        }
        // LOOPD(i);
        // for(int j=0; j<s1_size; j++){
        //     DUMPL(dp[j]);
        // }
        // cout << "   ";
        // for(int j=0; j<s2_size; j++){
        //     cout << setw(2) << s2[i][j] << ' ';
        // }
        // cout << endl;

        // for(int j=0; j<s1_size; j++){
        //     cout << setw(2) << s1[i][j] << ' ';
        //     for(int k=0; k<s2_size; k++){
        //         cout << setw(2) << dp[j][k] << ' ';
        //     }
        //     cout << endl;
        // }

        ans[i] = dp.back().back();
    }
    
    // DUMP(ans);
    for(int i=0; i<q; i++) cout << ans[i] << endl;
    return 0;
}

C++: #include <cstdio>
#include <algorithm>
#include <vector>
#include <string>

class TLcsSolver
{
    typedef std::vector<std::vector<int> > TTable;
    TTable FTable;
    std::string FArray1;
    std::string FArray2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < FTable.size(); ++i){
            for(std::size_t j = 1; j < FTable[i].size(); ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
public:
    TLcsSolver(const std::string& a, const std::string& b)
     : FArray1(a), FArray2(b)
     , FTable(a.size()+1, std::vector<int>(b.size()+1, 0))
    {
        Solve();
    }
    int Answer()const
    {
        return FTable[FArray1.size()][FArray2.size()];
    }
};

int main()
{
    int n;
    std::cin >> n;
    char a[1000];
    char b[1000];
    for(int i = 0; i < n; ++i){
        std::gets(a);
        std::gets(b);
        std::printf("%d", TLcsSolver(a, b).Answer());
    }
    return 0;
}