Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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