Go: package main

/*
最長共通部分列
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

C++: #include <iostream>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <bitset>
#include <iomanip>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define reps(i,s,e) for(int i=s; i<(int)e; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Graph = vector<vector<T>>;

void printFixedDouble(double d, int fix) {
    cout << std::fixed;
    cout << std::setprecision(fix) << d << endl;
}

int main() {
    int datanum;
    cin >> datanum;
    rep(d, datanum) {
        string a, b;
        cin >> a >> b;

        int aLen = a.length();
        int bLen = b.length();

        int dp[aLen + 1][bLen + 1];
        rep(i, aLen + 1) {
            dp[i][0] = 0;
        }
        rep(i, bLen + 1) {
            dp[0][i] = 0;
        }

        rep(i, aLen) {
            rep(j, bLen) {
                if (a[i] == b[j]) {
                    dp[i+1][j+1] = std::max(std::max(dp[i+1][j], dp[i][j+1]), dp[i][j] + 1);
                } else {
                    dp[i+1][j+1] = std::max(dp[i+1][j], dp[i][j+1]);
                }
            }
        }

        cout << dp[aLen][bLen] << endl;
    }
}

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

using namespace std;
const int L = 1001;
const int INF = -1001001001;

int main()
{
    string s, t;
    int dp[L][L];
    int q;
    cin >> q;
    for(int i=0;i<q;++i)
    {
        cin >> s;
        cin >> t;
        for(int j=0;j<s.size()+1;++j) dp[j][0] = 0;
        for(int j=0;j<t.size()+1;++j) dp[0][j] = 0;
        for(int j=1;j<s.size()+1;++j)
        {
            for(int k=1;k<t.size()+1;++k)
            {
                if(s[j-1] == t[k-1])
                {
                    dp[j][k] = max(max(dp[j][k-1], dp[j-1][k-1] + 1), dp[j-1][k]);
                }
                else
                {
                    dp[j][k] = max(dp[j][k-1], dp[j-1][k]);
                }
            }
        }
        cout << dp[s.size()][t.size()] << endl;
    }
}

C++: #include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define MAX_q 150
#define MAX_L 1000
string X, Y;
int dp[MAX_L + 1][MAX_L + 1];
void DP() {
    rep(i, X.size()) {
        rep(k, Y.size()) { dp[i][k] = 0; }
    }
    rep(i, X.size()) {
        rep(k, Y.size()) {
            if (X[i] == Y[k]) {
                dp[i + 1][k + 1] = dp[i][k] + 1;
            } else {
                dp[i + 1][k + 1] = max(dp[i][k + 1], dp[i + 1][k]);
            }
        }
    }
}
int main() {
    int q;
    cin >> q;
    rep(j, q) {
        cin >> X >> Y;
        DP();
        cout << dp[X.size()][Y.size()] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[1001][1001];




	
int main (){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	//input
	int n;
	cin >> n;
	vector<string> S(n);
	vector<string> T(n);
	for(int i=0;i<n;i++)cin >> S[i] >> T[i];


	//alorithm
	for(int i=0;i<n;i++){
		string s,t;
		s=S[i];
		t=T[i];
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=s.size();i++){
			for(int j=1;j<=t.size();j++){
				if(s[i-1]==t[j-1]){dp[i][j]=dp[i-1][j-1]+1;continue;}
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout << dp[s.size()][t.size()] << endl;
	}
}