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++: //最長共通部分列
//要は何枚のコインで支払えますか問題。

// lcmとかはg++ -std=c++17 default.cppみたいなかんじで
//vector<vector<ll>> piyo(tate,vector<ll> (yoko));

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1000000019
#define ll long long
#define ln cout<<endl;
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define double long double
#define REP(i,m,n) for(ll i=(ll)(m);i<(ll)(n);i++)
#define rep(i,n) REP(i,0,n)
using namespace std;
int dp[2000][2000];
vector<int> w(2000);
vector<int> v(2000);

int main() {
    cin.tie(0);
   	ios::sync_with_stdio(false);
    //double mini=INF;
    int maxi=0,f=0,sum=0;
    int i;
    double h;
    int n,x,m;
    int a,b,d,c;
    //int e,weight;
    int target;
    //targetが目的値、nが個数
    cin>>n;

    rep(_,n){
        string str1,str2;
        cin>>str1>>str2;

        //DPテーブルを0で初期化
        for(ll i=0;i<str1.length()+1;i++){
            for(ll j=0;j<str2.length()+2;j++){
                dp[i][j]=0;
            }
        }
        rep(i,str1.length()){
            rep(j,str2.length()){
                if(str1[i]==str2[j]){
                    dp[i+1][j+1]=dp[i][j]+1;
                }else{
                    dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
                }
            }
        }
        cout<<dp[str1.length()][str2.length()]<<endl;;
    }


    //dpテーブルのこたえにあたる部分を出力
    //今回は配るDPなのでn番目を出力しているところに注意!
    //cout<<dp[n][target]<<endl;


    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int p[1001][1001]={0};
int main(){
  int i,j,q,e,l,z,m,u,n;
  char x[2001],y[2001];
  cin>>q;
  for(e=0;e<q;e++){
    scanf("%s %s",x,y);
    l=strlen(x);
    z=strlen(y);
 
    for(i=0;i<=l;i++){
      for(j=0;j<=z;j++){
        p[i][j]=0;
      }
    }
    for(i=1;i<=z;i++){
      for(j=1;j<=l;j++){
        if(y[i-1]==x[j-1])p[j][i]=p[j-1][i-1]+1;
        else p[j][i]=max(p[j-1][i],p[j][i-1]);
      }
    }
    cout<<p[l][z]<<endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

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

C++: #include<iostream>
using namespace std;
int lcs(string a,string b)
{
    if(a.length()<=0||b.length()<=0)
    {
        return 0;
    }
    else if(a[a.length()-1]==b[b.length()-1])
    {
        return 1+lcs(a.substr(0,a.length()-1),b.substr(0,b.length()-1));
    }
    else
    {
        return max(lcs(a.substr(0,a.length()),b.substr(0,b.length()-1)),lcs(a.substr(0,a.length()-1),b.substr(0,b.length())));
    }
};
int main()
{
    int num;
    cin>>num;
    for(int i=0;i<num;i++)
    {
        string a,b;
        cin>>a>>b;
        cout<<lcs(a,b)<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#define eol '\n'
using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    string x,y;
    cin >> x >> y;

    int m,n;
    m = x.length();
    n = y.length();

    vector< vector<int> > mat(m+1, vector<int>(n+1,0));

    for (int sum = 2; sum <= m+n; sum++) {
      for (int j = 1; j < sum; j++) {
        int k = sum - j;
        if (k <= 0 || k > n || j > m) continue;

        int plus = x[j-1] == y[k-1] ? 1 : 0;
        mat[j][k] = max(max(mat[j-1][k], mat[j][k-1]), mat[j-1][k-1] + plus);
      }
    }

    cout << mat[m][n] << eol;
  }
  
  return 0;
}