Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

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;
}