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>
#include<map>
#include<vector>
using namespace std;
map <char,vector<int>> alx,aly;//x,y
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//yn文字目以降でX[xn]と同じやつを探す
    if(memo[xn][yn]!=-1)return memo[xn][yn];
    int ans=0;
	/*
    for(int i=xn; i<x.size();i++){
        for(int j=yn; j<y.size(); j++){
            if(alx[x[i]]==aly[y[j]]){
                ans=max(ans,lcs(i+1,j+1)+1);
            }
        }
    }
	*/
	for(int i=xn; i<x.size();i++){
        for(int j=0; j<aly[x[i]].size(); j++){
			if(aly[x[i]][j] < yn) continue;
			ans=max(ans,lcs(i+1,aly[x[i]][j]+1)+1);
			break;
		}
    }
    return memo[xn][yn]=ans;
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++){
				memo[i][j]=-1;
			}
        }

		alx.clear();
		aly.clear();

		for(int i=0; i<x.size(); i++) alx[x[i]].push_back(i);
		for(int i=0; i<y.size(); i++) aly[y[i]].push_back(i);
		

        //memset(memo, -1, sizeof(memo));
         
        cout << lcs(0,0) << endl;
 
    }
    return 0;
}



/*
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//Y[yn]とX[xn]を比較
    if(memo[xn][yn]!=-1)return memo[xn][yn];
	else if(xn==0 || yn==0)return 0;
	else if(x[xn]==y[yn])return memo[xn][yn] = lcs(xn-1,yn-1)+1;
	else  return memo[xn][yn] = max(lcs(xn,yn-1),lcs(xn-1,yn));
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++)memo[i][j]=-1;
        }
        //memset(memo, -1, sizeof(memo));
         
        cout << max(lcs(x.size(),y.size()),0) << endl;
 
    }
    return 0;
}*/

/*

#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n,memo[50]={};

int fib(int n){
	if(memo[n]!=0)return memo[n];
	return memo[n]=fib(n-1)+fib(n-2);
}


int main(){

	cin >> n;
	memo[0]=1;
	memo[1]=1;
	for(int i=2; i<=n; i++)
		memo[i]=memo[i-1]+memo[i-2];
	cout << memo[n] << endl;
	return 0;
}
*/

/*
#include<queue>
using namespace std;
int n,A[2000],q;//A:0origin
int memo[2000][2000];

bool solve(int i,int m){//i番目以降のAを使ってmを作る
	if(m==0) return true;//０はどんなときも作れる
	if(i==n) return false;//すべて使ってもつくれない
	//すでにやってるときメモを返す
	if(memo[i][m]!=0){
		if(memo[i][m]==-1)return false;
		else return true;
	}
	//solve(i,m)  A[i]を使う solve(i+1,m-A[i]) || 使わない　solve(i+1,m)
	bool X=false;
	if(m-A[i]>=0 && solve(i+1,m-A[i])) X=true;
	else if(solve(i+1,m)) X = true;

	if(X)memo[i][m]=1;
	else memo[i][m]=-1;

	 return X;

}

int main(){

	cin >> n;

	for(int i=0; i<n; i++) cin >> A[i];

	
	cin >> q;

	for(int i=0; i<q; i++){
		int m;
		cin >> m;
		for(int k=0; k<n; k++){
			for(int j=0; j<n; j++) memo[k][j]=0;
		}

		if(solve(0,m))cout << "yes" << endl;
		else cout << "no" << endl; 
	}

	return 0;
}

*/

/*
	bool dp[2000][2000];
	bool memo[2000][2000];//[順番][m-これまでの総和]
	
	bool solve(int now,int least){
		if(now == n) return false;//now>nならfalse
		else if(least == 0)return true;//least==0 >> true
		else if(memo[now][least])return dp[now][least];//already diside >> そのまま出来上がってるのを返す
		else if(solve(now+1,least))dp[now][least] = true;//A[now]をとらない >> solve(now++,least)
		else if(least-A[now] > 0 && solve(now++,least-A[now]))dp[now][least] = true;//A[now]をとる (least-A[now]>0 only)>> solve(now++,least-A[now])
		else dp[now][least] = false;
		memo[now][least] = true;
		return dp[now][least];
	}
	*/

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); (i)--)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
using namespace std;

string x, y;
int n, m;
int dp[1010][1010];

int solve(int i, int j){
    if(i==0 || j==0) return dp[i][j] = 0;
    if(dp[i][j] >= 0) return dp[i][j];
    if(x[i-1] == y[j-1]){
        dp[i][j] = max(solve(i-1, j), solve(i, j-1));
        return dp[i][j] = max(dp[i][j], solve(i-1, j-1)+1);
    }
    return dp[i][j] = max(solve(i-1, j), solve(i, j-1));
}

int main(){
    int q; cin >> q;
    rep(i, q){
        cin >> x >> y;
        n = (int)(x.size()); m = (int)(y.size());
        rep(i, n+1)rep(j, m+1) dp[i][j] = -1;
        cout << solve(n, m) << endl;
    }
}