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>
#include <algorithm>

using namespace std;
int main(){
  int dataset, i, j, k, alen, blen;
  string a, b;
  cin >> dataset;
  for (i = 0; i < dataset; ++i){
    cin >> a >> b;
    alen = a.length();
    blen = b.length();
    int lcs[alen+1][blen+1];
    for (j = 0; j <= alen; ++j){
      for (k = 0; k <= blen; ++k){
        lcs[j][k] = 0;
      }
    }
    for (j = 1; j <= alen; ++j){
      for (k = 1; k <= blen; ++k){
        if(a.at(j-1) == b.at(k-1)){
          lcs[j][k] = lcs[j-1][k-1] + 1;
        }else{
          lcs[j][k] = max(lcs[j-1][k], lcs[j][k-1]);
        }
      }
    }
    cout << lcs[alen][blen] << endl;
  }
}

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

C++: #include<cstdio>
#include<cstring>
#define N 1000

int LCS[N+1][N+1];

int LCSLength(char X[], int m, char Y[], int n){
	for(int i=0;i<=m;i++)
		LCS[i][n] = 0;
	for(int j=0;j<=n;j++)
		LCS[m][j] = 0;
	for(int i=m-1;i>=0;i--){
		for(int j=n-1;j>=0;j--){
			LCS[i][j] = LCS[i+1][j+1];
			if(X[i] == Y[j])
				LCS[i][j]++;
			if(LCS[i][j+1] > LCS[i][j])
				LCS[i][j] = LCS[i][j+1];
			if(LCS[i+1][j] > LCS[i][j])
				LCS[i][j] = LCS[i+1][j];
		}
	}
	return LCS[0][0];
}

int main(){
	char X[N+1],Y[N+1];
	int num;
	scanf("%d",&num);
	for(int i=1;i<=num;i++){
		scanf("%s %s",X,Y);
		int m = strlen(X);
		int n = strlen(Y);
		printf("%d\n",LCSLength(X,m,Y,n));
	}
	return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int main()
{
    int n;
    cin >> n;
    rep(k, n)
    {
        string s, t;
        cin >> s >> t;
        int s_s = s.size(), t_s = t.size();
        int dp[s_s + 1][t_s + 1];
        rep(i, s_s + 1) dp[i][0] = 0;
        rep(j, t_s + 1) dp[0][j] = 0;

        rep(i, s_s)
        {
            rep(j, t_s)
            {
                if (s[i] == t[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]);
            }
        }
        OP(dp[s_s][t_s]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define loop(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
using ll = int64_t;

int main(){
    int q; cin >> q;
    rep(qi, q){
        string x, y; cin >> x >> y;
        int xsize = x.size(), ysize = y.size();
        int dp[xsize+1][ysize+1];
        memset(dp, 0, sizeof(dp));
        loop(i, 1, xsize+1)loop(j, 1, ysize+1){
            if(x[i-1] == y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
        cout << dp[xsize][ysize] << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int dp[1002][1002];

int main()
{
	for (int i = 0; i < 1002; i++)
	{
		dp[0][i] = 0;
		dp[i][0] = 0;
	}
	int N;
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		string str1, str2;
		cin >> str1 >> str2;
		for (int i = 1; i <= str1.size(); i++)
		{
			for (int j = 1; j <= str2.size(); j++)
			{
				if (str1[i - 1] == str2[j - 1])dp[i][j]=dp[i - 1][j - 1] + 1;
				else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		cout << dp[str1.size()][str2.size()] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
using namespace std;

int dp[1001][1001];
string str1, str2;
int n;

int solve(string str1, string str2) {
  memset(dp, 0, sizeof(dp));
  for(int r = 0; r < str2.size(); r++) {
    for(int c = 0; c < str1.size(); c++) {
      if(str2[r] == str1[c]) {
        dp[r + 1][c + 1] = dp[r][c] + 1;
      }
      else {
        dp[r + 1][c + 1] = max(dp[r + 1][c], dp[r][c + 1]);
      }
    }
  }
  return dp[str2.size()][str1.size()];
}

int main() {
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> str1 >> str2;
    cout << solve(str1, str2) << endl;
  }
}