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 <algorithm>
#include <complex>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define SAY_YES cout << "YES" << endl;
#define SAY_Yes cout << "Yes" << endl;
#define SAY_NO cout << "NO" << endl;
#define SAY_No cout << "No" << endl;
#define IFYES(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "YES" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "NO" << endl;  \
    }
#define IFYes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "Yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "No" << endl;  \
    }
#define IFyes(TRUE_OR_FALSE)   \
    if (TRUE_OR_FALSE)         \
    {                          \
        cout << "yes" << endl; \
    }                          \
    else                       \
    {                          \
        cout << "no" << endl;  \
    }
#define DEBUG_OUTPUT_ARRAY(XXX, ONE)                            \
    for (int i = 0; i < (ONE); i++)                             \
    {                                                           \
        cout << "DEBUG: i = " << i << " -> " << XXX[i] << endl; \
    }
#define DEBUG_OUTPUT_ARRAY2(XXX, ONE, TWO)                             \
    for (int i = 0; i < (ONE); i++)                                    \
    {                                                                  \
        cout << "<<< i = " << i << " >>>" << endl;                     \
        for (int j = 0; j < (TWO); j++)                                \
        {                                                              \
            cout << "DEBUG: j = " << j << " -> " << XXX[i][j] << endl; \
        }                                                              \
    }
#define DEBUG_OUTPUT_ARRAY2_BOX(XXX, ONE, TWO) \
    for (int i = 0; i < (ONE); i++)            \
    {                                          \
        cout << i << "  ";                     \
        for (int j = 0; j < (TWO); j++)        \
        {                                      \
            cout << XXX[i][j] << " ";          \
        }                                      \
        cout << endl;                          \
    }

typedef pair<long long int, long long int> pll;


const long long int mod = 1000000007;
const long long int INF = 99999999999999999;



class LCS{
    public:
    long long int **dp;
    long long int **longest;
    long long int result;
    string LCSstr;
    LCS(long long int one,long long int two){
        dp=new long long int*[one];
        longest=new long long int *[one];
        for(int i=0;i<one;i++){
            dp[i]=new long long int[two];
            longest[i]=new long long int[two];
        } 
    }
    void DoLCS(string str1,string str2){
        LCSstr.clear();
        for(int i=0;i<str1.length();i++){
            for(int j=0;j<str2.length();j++){
                dp[i][j]=0;
                longest[i][j]=0;
            }
            
        }

        for(int i=0;i<str1.length();i++){
            if(str1[i]==str2[0]){dp[i][0]=1;}
            else{dp[i][0]=0;}
            for(int j=1;j<str2.length();j++){
                if(i!=0)dp[i][j]=longest[i-1][j-1];
                if(str1[i]==str2[j]){dp[i][j]++;}
            }
            
            if(i!=0){
                longest[i][0]=max({longest[i-1][0],dp[i][0]});
                for(int j=1;j<str2.length();j++){
                    longest[i][j]=max({longest[i-1][j],dp[i][j],longest[i][j-1]});
                }
            }else{
                longest[0][0]=dp[i][0];
                for(int j=1;j<str2.length();j++){
                    longest[0][j]=max({dp[0][j],longest[0][j-1]});
                }
            }
        }

        pair<long long int ,long long int>rev;
        rev={str1.length(),str2.length()};
        long long int now=longest[str1.length()-1][str2.length()-1];
        bool key=false;
        //cout<<now<<endl;
        //DEBUG_OUTPUT_ARRAY2_BOX(dp,rev.first,rev.second);
        while(now!=0){
            key=false;
            for(int j=0;j<rev.first;j++){
                if(now==dp[j][rev.second-1]&&(now-1==longest[max(j-1,0)][max(rev.second-1-1,0LL)]||j==0||rev.second-1==0)){
                    
                    LCSstr.push_back(str1[j]);
                    rev={j,rev.second-1};
                    now--;
                    key=true;
                    //cout<<"REV>>"<<rev.first<<" "<<rev.second<<endl;
                    break;
                }
            }
            if(key){continue;}
            for(int j=0;j<rev.second;j++){
                if(now==dp[rev.first-1][j]&&(now-1==longest[max(rev.first-1-1,0LL)][max(j-1,0)]||rev.first-1==0||j==0)){
                    now--;
                    LCSstr.push_back(str2[j]);
                    rev={rev.first-1,j};
                    key=true;
                    
                    //cout<<"REV>>"<<rev.first<<" "<<rev.second<<endl;
                    break;
                }
            }
            if(!key){
                rev={rev.first-1,rev.second-1};
            }        
        }
        reverse(LCSstr.begin(),LCSstr.end());
        result=LCSstr.length();
    }
    ~LCS(){}
};

long long int N,M,W,dp[55000]={},c[10005],w[105];
string str[350],str2[350];

int main()
{
    cout << fixed << setprecision(18);
    cin>>N;
    
    LCS lcs(1005,1005);
    for(long long int i = 1; i <= N; i++){
        cin>>str[i]>>str2[i];
        

        lcs.DoLCS(str[i],str2[i]);
        cout<<lcs.LCSstr.length()<<endl;
        
    }
     
        
        
    
    
}

C++: // ALDS1_10_C.cpp
// Dynamic Programming - Longest Common Subsequence

#include <iostream>
#include <vector>
#include <string>
using namespace std;

int LCS(string s, string t)
{
    int res = 0;
    if (s.empty() || t.empty())
        return res;
    string s1(s.begin() + 1, s.end());
    string t1(t.begin() + 1, t.end());
    if (s[0] == t[0]) {
        res = 1 + LCS(s1, t1);
    }
    else {
        res = max(LCS(s1, t), LCS(s, t1));
    }
    return res;
}

int main()
{
    int n;
    cin >> n;
    string s, t;
    while (n--) {
        cin >> s >> t;
        int res = LCS(s, t);
        cout << res << endl;
    }
    return 0;
}