Go: package main

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

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 {
	lenX, lenY := len(strX), len(strY)
	memo := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		memo[i] = make([]int, lenY+1)
	}

	lcs := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if strX[i-1] == strY[j-1] {
				memo[i][j] = memo[i-1][j-1] + 1
			} else {
				if memo[i-1][j] > memo[i][j-1] {
					memo[i][j] = memo[i-1][j]
				} else {
					memo[i][j] = memo[i][j-1]
				}
			}
			if memo[i][j] > lcs {
				lcs = memo[i][j]
			}
		}
	}
	return lcs
}

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>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

int lcs(string x, string y) {
    x = ' ' + x;
    y = ' ' + y;
    int m = x.size();
    int n = y.size();
    vector<vector<int>> c(m, vector<int>(n, 0));
    rep(i, m) {
        rep(j, n) {
            if (i == 0 || j == 0) continue;
            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]);
        }
    }
    return c[m-1][n-1];
}

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

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

string X;
string Y;

int calc(int x, int y, vector<vector<int> >& Memo){
    if (X[x] == '\0' || Y[y] == '\0') return 0;
    if (Memo[x][y] != -1) return Memo[x][y];

    int unuse = calc(x + 1, y, Memo);
    int use = 0;
    int y_target = Y.find(X[x], y);
    if (y_target == string::npos) {
        use = 0;
    } else {
        use = calc(x + 1, y_target + 1, Memo) + 1;
    }
    return Memo[x][y] = max(unuse, use);
}

int main(){
    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        cin >> X;
        cin >> Y;
        vector<int> tmp(Y.size(), -1);
        vector<vector<int> > Memo(X.size(), tmp);
        cout << calc(0, 0, Memo) << endl;
    }

    return 0;
}

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

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

    int max_len = 0;
    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) {
            if (X[i - 1] == Y[j - 1]) {
                c[i][j] = c[i - 1][j - 1] + 1;
            } else {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            max_len = max(max_len, c[i][j]);
        }
    }

    return max_len;
}

int main() {
    int n;
    cin >> n;
    string s1, s2;
    int max_len[n];
    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        max_len[i] = lcs(s1, s2);
    }

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

    return 0;
}

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


int LCS(const string& a, const string& b) {
    int ans = 0;
    const int len_a = a.length(), len_b = b.length();

    int dp[len_a + 1][len_b + 1] = {};
    for(int i=1; i<=len_a; i++) {
	for(int j=1; j<=len_b; j++) {
	    dp[i][j] = max(dp[i][j], dp[i-1][j] );
	    dp[i][j] = max(dp[i][j], dp[i][j-1] );
	    dp[i][j] = max(dp[i][j], dp[i-1][j-1] + (a[i-1] == b[j-1] ? 1 : 0) );
	}
    }
    ans = dp[len_a][len_b];
    return ans;
}

int main() {

    int N;
    cin >> N;

    for(int i=0; i<N; i++) {
	string a,b;
	cin >> a >> b;
	cout << LCS(a,b) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define N 1000
using namespace std;
 
int lcs(string S1,string S2){
    int c[N+1][N+1];
    int S1_size = S1.size();
    int S2_size = S2.size();
     
    for(int i=1;i<S1_size;i++) c[i][0] = 0;
    for(int i=1;i<S2_size;i++) c[0][i] = 0;
     
    S1 = ' '+S1;
    S2 = ' '+S2;
     
    int max_lcs=0;
    for(int j=1;j<=S1_size;j++){
        for(int k=1;k<=S2_size;k++){
            if(S1[j]==S2[k]){
                c[j][k] = c[j-1][k-1] + 1;
            }else{
                c[j][k] = max(c[j-1][k],c[j][k-1]);
            }
            max_lcs = max(max_lcs,c[j][k]);
        }
    }
    return max_lcs;
}
 
int main() {
    // your code goes here
    int q;
    string str1,str2;
    cin>>q;
    for(int i=0;i<q;i++){
        cin>>str1>>str2;
        cout<<lcs(str1,str2)<<endl;
    }
    return 0;
}

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

int Max(int &a,int b){return a=max(a,b);}

int LCS(string &a,string &b){
  int n = a.size();
  int m = b.size();
  vector <vector<int> > dp(n+1,vector<int>(m+1));

  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++){
      if(i<n && j<m && a[i] == b[j]) Max(dp[i+1][j+1],dp[i][j]+1);
      if(i<n)Max(dp[i+1][j],dp[i][j]);
      if(j<m)Max(dp[i][j+1],dp[i][j]);

    }
  return dp[n][m];
}

int main(){
  int q;
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    cout<<LCS(a,b)<<endl;
  }
  return 0;
}

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

// ???????????§???????????§?????§??????
int DP[1024][1024] = {};

int main(){
    int N;
    cin >> N;
    for(int c=0; c<N; ++c){
        string X,Y;
        cin >> X >> Y;
        int xlen = (int)X.size();
        int ylen = (int)Y.size();

        for(int i=0;i<=xlen;++i){
          DP[i][0] = 0;
        }
        for(int i=0;i<=ylen;++i){
          DP[0][i] = 0;
        }

        for(int i=1;i<=xlen;++i){
            for(int j=1;j<=ylen;++j){
                if(X[i-1] == Y[j-1]){ // X???i???????????¨Y???j?????????????????????????????????index?????????????????¨?????¨??????
                    DP[i][j] = DP[i-1][j-1] + 1;
                } else {
                    DP[i][j] = max(DP[i-1][j], DP[i][j-1]);
                }
            }
        }
        cout << DP[xlen][ylen] << endl;
    }
}

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

int LCS(int len[],char S[][1001]){//???????????????2?????§?????????
    int DP[1001][1001] = {};
    for (int i = 1; i <= len[0]; i++) {
        for (int j = 1; j <= len[1]; j++) {
            if(S[0][i-1] == S[1][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[len[0]][len[1]];
}

void solve(){
    char S[2][1001];
    int len[2];
    scanf("%s %s",S[0],S[1]);
    len[0] = strlen(S[0]);
    len[1] = strlen(S[1]);
    int ll;
    ll = LCS(len,S);
    printf("%d\n",ll);
}

int main(){
    int N;
    cin >> N;
    for (int i = 0; i < N; i++) {
        solve();
    }
    
    return 0;
}