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 <cstdio>
#include <cstring>
#include <algorithm>

class TLcsSolver
{
    int FTable[1001][1001];
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    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 < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
    void Print()const
    {
        for(std::size_t i = 0; i < Fn1; ++i){
            for(std::size_t j = 0; j < Fn2; ++j){
                printf("%d\t", FTable[i][j]);
            }
            printf("\n");
        }
    } 
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[Fn1-1][Fn2-1];
    }
};

int main()
{
    int n;
    std::scanf("%d\n", &n);
    char a[1001];
    char b[1001];
    for(int i = 0; i < n; ++i){
        std::fgets(a, 1001, stdin);
        std::fgets(b, 1001, stdin);
        std::printf("%d\n", TLcsSolver(a, b).Answer());
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define For(I,A,B) for(int I=(A); I < (B); ++I)
int c[1005][1005];
int main()
{
int t;
cin>>t;
while(t--){

int x,y,z,k=0,l,i,j;
string a,b;

 For(i,0,1005)
 For(j,0,1005)
 c[i][j]=0;
 getline(cin,a);
 getline(cin,b);
 x=a.length();
 y=b.length();
 For(i,0,x)
 For(j,0,y)
 {
     if(a[i]==b[j]) c[i][j]=c[i-1][j-1]+1;
     else  c[i][j]=max(c[i-1][j],c[i][j-1]);
 }
    cout<<c[x][y]<<endl;

}

    return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
int main(void){
    int cnt;
    int n;
    std::cin>>n;

    for(int k=0;k<n;k++){
        std::string str1;
        std::string str2;
        std::cin>>str1;
        std::cin>>str2;
        int l1=str1.size();
        int l2=str2.size();
        int A[l1][l2]={};

        for(int i=0;i<l1;i++){
            for(int j=0;j<l2;j++){
                if(i==0 or j==0){
                    if(str1[i]==str2[j]){
                        A[i][j]=1;
                    }else{
                        A[i][j]=0;
                    }
                }else if(str1[i]==str2[j]){
                    A[i][j]=A[i-1][j-1]+1;
                }else{
                    A[i][j]=std::max(A[i][j-1],A[i-1][j]);
                }
                //std::cout<<A[i][j]<<" ";
            }
            //std::cout<<"\n";
        }
        std::cout<<A[l1-1][l2-1]<<"\n";
    }
    return 0;
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1001][1001];

int main(){
	int n; cin >> n;
	while (n--){
		string X, Y; cin >> X >> Y;
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= X.size(); ++i){
			for (int j = 1; j <= Y.size(); ++j){
				if (X[i] == Y[j]) dp[i][j] = max(dp[i][j-1],max(dp[i-1][j],dp[i-1][j-1]+1));
				else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
			}
		}
		cout << dp[(int)X.size()][(int)Y.size()] << '\n';
		for (int i = 0; i <= X.size(); ++i){
			for (int j = 0; j <= Y.size(); ++j){
				cout << dp[i][j] << ' ';
			}
			cout << '\n';
		}
	}
}

C++: #include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int q, dp[1007][1007]; string s, t;
int main() {
	cin >> q;
	while(q--) {
		cin >> s >> t;
		for(int i = 0; i < s.size(); i++) {
			for(int j = 0; j < t.size(); j++) {
				dp[i + 1][j + 1] = (s[i] == t[j] ? dp[i][j] + 1 : max(dp[i][j + 1], dp[i + 1][j]));
			}
		}
		cout << dp[s.size()][t.size()] << endl;
	}
}

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

int lcs(string str1, string str2) {
	int n1 = str1.size();
	int n2 = str2.size();
        int maxl = 0;
	int A[n1 + 1][n2 + 1];
	for (int i = 0; i <= n1; i++) A[i][0] = 0;
	for (int j = 1; j <= n1; j++) A[0][j] = 0;

	for (int i = 1; i <= n1; i++) {
		for (int j = 1; j <= n2; j++) { 
			if (str1[i - 1] == str2[j - 1]) {
				A[i][j] = A[i - 1][j - 1] + 1; 
			} else {
				A[i][j] = max(A[i - 1][j], A[i][j - 1]); 
			}
                        maxl = max(maxl, A[i][j]);
		}
	}
	return A[n1][n2];
}

int main() {
	int n;
	string str1, str2;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> str1 >> str2;
		cout << lcs(str1, str2) << endl;
	}
	return 0;
}

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

int lcs(string X, string Y) {
    int m = X.length();
    int n = Y.length();
    X = ' ' + X;
    Y = ' ' + Y;
    vector<vector<int>> dp(2000 + 1, vector<int>(2000 + 1, 0));

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

    return dp[m][n];
}

int main() {
    int q;
    cin >> q;
    for (int t = 0; t < q; t++) {
        string X, Y;
        cin >> X >> Y;
        cout << lcs(X, Y) << endl;
    }

    return 0;
}

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

using namespace std;

int solve(string s1, string s2){
    int L[s1.size()+1][s2.size()+1];
    for(int i=0;i<s1.size();i++){
        for(int j=0;j<s2.size();j++){
            if(i==0 || j==0){
                L[i][j]=0;
            }
            else if(s1[i] == s2[j]){
                L[i][j] = 1 + L[i-1][j-1];
            }
            else{
                L[i][j] = max(L[i][j-1],L[i-1][j]);
            }
        }
    }
    return L[s1.size()][s2.size()];
}

int main(){
    int n ; cin >> n;
    for(int i=0;i<n;i++){
        string s1,s2; cin>> s1 >> s2;\
        cout << solve(s1,s2) << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
int dp[1001][1001];

int main(){
	int n; cin >> n;
	while (n--){
		string X, Y; cin >> X >> Y;
		memset(dp, 0, sizeof(dp));
		for (int i = 1; i <= X.size(); ++i){
			for (int j = 1; j <= Y.size(); ++j){
				if (X[i] == Y[j]) dp[i][j] = max(dp[i][j-1],max(dp[i-1][j],dp[i-1][j-1]+1));
				else dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);
			}
		}
		cout << dp[(int)X.size()][(int)Y.size()] << '\n';
	}
}