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 <string>

#define _For(idx, from, to, inc, cmp) \
	for(auto idx=(from) ; idx cmp (to) ; inc idx)
#define ForLE(idx, from, to) \
	_For(idx, from, to, ++, <=)
#define For(idx, from, to) \
	_For(idx, from, to, ++, <)

constexpr int MaxLen = 1000;
int Memo[MaxLen+1][MaxLen+1];
int main() {
	std::string line;
	const auto nextline = [&line](){
		std::getline(std::cin, line);
	};
	nextline();
	const int NQ = std::stoi(line);
	For(i, 0, NQ) {
		std::string s0, s1;
		std::getline(std::cin, s0);
		std::getline(std::cin, s1);

		int len0 = s0.length(),
			len1 = s1.length();
		if(len0 < len1) {
			std::swap(s0, s1);
			std::swap(len0, len1);
		}
		ForLE(j, 0, len0)
			Memo[j][0] = 0;
		ForLE(j, 0, len1)
			Memo[0][j] = 0;

		ForLE(j, 1, len1) {
			ForLE(k, 1, len0) {
				int res = std::max(
								Memo[k][j-1],
								Memo[k-1][j]
							);
				if(s0[k-1] == s1[j-1])
					res = std::max(Memo[k-1][j-1] + 1, res);
				Memo[k][j] = res;
			}
		}
		std::cout << Memo[len0][len1] << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	int q;cin>>q;
	for(int i = 0; i < q; i++) {
		string x,y;cin>>x>>y;
		vector<vector<int>> dp(1001,vector<int>(1001,0));
		for(int i = 0; i < x.size(); i++) {
			for(int j = 0; j < y.size(); j++) {
				if (x[i] == y[j]){
					dp[i + 1][j + 1] = max(dp[i][j] + 1,max(dp[i][j + 1],dp[i + 1][j]));
				}
				else{
					dp[i + 1][j + 1] = max(dp[i][j + 1],dp[i + 1][j]);
				}
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <map>
#include <iterator>
#include <algorithm>

#define X first
#define Y second

using namespace std;

int q;
vector<pair<string,string>> dataset;

int ans(pair<string, string> &data)
{
    array<array<int,1001>,1001> dp = {};
    for (unsigned int i=1; i<=data.X.length(); ++i) {
        for (unsigned int j=1; j<=data.Y.length(); ++j) {
            if (data.X[i-1] == data.Y[j-1]) dp[i][j] = max({dp[i-1][j-1] + 1, dp[i-1][j], dp[i][j-1]});
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    return dp[data.X.length()][data.Y.length()];
}

int main(int argc, char **argv)
{
    cin >> q;
    for (int i=0; i<q; ++i) {
        dataset.emplace_back(std::make_pair("",""));
        cin >> dataset[i].X;
        cin >> dataset[i].Y;
    }
    for (int i=0; i<q; ++i) {
        cout << ans(dataset[i]) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    string x, y;
    cin >> x >> y;
    int m = x.length();
    int n = y.length();
    vector<vector<int>> dp(m + 1, vector<int>(n + 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]);
        }
      }
    }
    int ret = dp[m][n];
    cout << ret << '\n';
  }
  return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

// str1 should be shorter
int solve(const string& str1, const string& str2) {
  vector<int> cur(str2.size(), 0);
  vector<int> prev;
  int max_ = 0;
  for (auto it = str1.begin(); it != str1.end(); ++it) {
    prev = cur;
    for (size_t i = 0; i < str2.size(); ++i) {
      if (*it == str2[i]) {
        auto p = i == 0 ? 0 : prev[i - 1];
        cur[i] = p + 1;
        max_ = max(max_, cur[i]);
      }
    }
  }
  return max_;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string str1;
    string str2;
    cin >> str1 >> str2;
    auto res = (str1.size() <= str2.size()) ? solve(str1, str2) : solve(str2, str1);
    cout << res << endl;
  }
}

C++: #include <bits/stdc++.h>
using namespace std;
int q;
string x, y;
int m, n;
int a[1000][1000];
int main()
{
	cin >> q;
	for(int i=0; i<q; i++)
	{
		cin >> x >> y;
		m=x.size();
		n=y.size();
		if(x[0]==y[0])
		{
			a[0][0]=1;
		}
		else
		{
			a[0][0]=0;
		}
		for(int j=1; j<m+n-1; j++)
		{
			if((j<n)&&((x[0]!=y[j])&&(a[0][j-1]==0)))
			{
				a[0][j]=0;
			}
			else if(j<n)
			{
				a[0][j]=1;
			}
			if((j<m)&&((x[j]!=y[0])&&(a[j-1][0]==0)))
			{
				a[j][0]=0;
			}
			else if(j<m)
			{
				a[j][0]=1;
			}
			for(int k=max(1, j-n+1); k<min(j, m); k++)
			{
				if(x[k]==y[j-k])
				{
					a[k][j-k]=a[k-1][j-k-1]+1;
				}
				else
				{
					a[k][j-k]=max(a[k-1][j-k], a[k][j-k-1]);
				}
			}
		}
		cout << a[m-1][n-1] << endl;
	}
	return 0;
}