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++: /*
　 　 　 　 　 　　/T￣「ヽ
　 　 　 　 　 　 │ ｌ　|　|
　 　　　　　　　　|　l　l　!
　 　 　 　 　 　 　l　l　! L_
　 　 　　　　　／ヽ　　　　｀丶 　　　　, -─┐
　　　　　　　/　ｎ　　ｎ 　　,r‐ヽ 　 ／　,r─,'
　 　　　ャ‐ﾊ 　ﾋ! 　 ﾋ! 　/ 　 └ｧ′／ 　/
　 　　　ム ヽ! ＿'__'_　　j 　　　く ／ 　　/
　 　 　 　 ヽﾍ! 　 　　￣（-‐ヽrｰﾄ──/
　 　　　　　　　｀Tヽ､　弋 　 Y　　ト--'
　 　　　　　　　　ｊ_,_,ﾉ￣｀j_,_,ﾉ｀'‐'′No.258:ミズゴロウ
*/
//AOJだとboostライブラリは使えない...(悲しい)
//#include <boost/multiprecision/cpp_int.hpp>
//namespace mp = boost::multiprecision;
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
//using INT = mp::cpp_int;
using namespace std;
using ll = long long;
using vec = vector<ll>;
using vect = vector<double>;
using Graph = vector<vector<ll>>;
#define loop(i, n) for (ll i = 0; i < n; i++)
#define Loop(i, m, n) for (ll i = m; i < n; i++)
#define pool(i, n) for (ll i = n; i >= 0; i--)
#define Pool(i, m, n) for (ll i = n; i >= m; i--)
#define mod 1000000007ll;
#define setbit bitset<8>
#define flagcount __builtin_popcount
#define flag(x) (1 << x)
#define flagadd(bit, x) bit |= flag(x)
#define flagpop(bit, x) bit &= ~flag(x)
#define flagon(bit, i) bit &flag(i)
#define flagoff(bit, i) !(bit & (1 << i))
#define all(v) v.begin(), v.end()
#define low2way(v, x) lower_bound(all(v), x)
#define high2way(v, x) upper_bound(all(v), x)
#define count2way(v, x) high2way(v, x) - low2way(v, x)
#define lower(v, x) low2way(v, x) - v.begin()       //1番左が0、もし見つから無いならｎを出力
#define higher(v, x) high2way(v, x) - v.begin() - 1 //1番左が0、もし見つからないならn-1を出力（注意）
#define putout(a) cout << a << endl
#define putout2(a, b) \
  putout(a);          \
  putout(b)
#define putout3(a, b, c) \
  putout(a);             \
  putout(b);             \
  putout(c)
#define putout4(a, b, c, d) \
  putout(a);                \
  putout(b);                \
  putout(c);                \
  putout(d)
#define putout5(a, b, c, d, e) \
  putout(a);                   \
  putout(b);                   \
  putout(c);                   \
  putout(d);                   \
  putout(e)
#define Gput(a, b) G[a].push_back(b)
#define cin1(a) cin >> a
#define cin2(a, b) cin >> a >> b
#define cin3(a, b, c) cin >> a >> b >> c
#define cin4(a, b, c, d) cin >> a >> b >> c >> d
#define cin5(a, b, c, d, e) cin >> a >> b >> c >> d >> e
#define sum(v) accumulate(all(v), 0ll)
#define gcd(x, y) __gcd(x, y)
ll ctoi(char c)
{
  if (c >= '0' && c <= '9')
  {
    return c - '0';
  }
  return 0;
}
ll lcm(ll x, ll y)
{
  ll z = gcd(x, y);
  return x * y / z;
}
/*
INT LCM(INT x, INT y)
{
  INT z = gcd(x, y);
  return x * y / z;
}
*/
ll primejudge(ll n)
{
  if (n < 2)
    return 0;
  else if (n == 2)
    return 1;
  else if (n % 2 == 0)
    return 0;
  double sqrtn = sqrt(n);
  Loop(i, 3, sqrtn + 1)
  {
    if (n % i == 0)
    {
      return 0;
    }
    i++;
  }
  return 1;
}
/*
INT modinv(INT a, INT m)
{
  INT b = m, u = 1, v = 0;
  while (b)
  {
    INT t = a / b;
    a -= t * b;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  u %= m;
  if (u < 0)
    u += m;
  return u;
}
*/
//場合によって使い分ける
//const ll dx[4]={1,0,-1,0};
//const ll dy[4]={0,1,0,-1};
const ll dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};

/*
//TIMESTAMPbfs
vector<bool> seen;
vec first;
vec last;
void dfs(const Graph &G,ll v,ll &time){
  if(!(seen[v])){
  seen[v]=true;
  first[v]=time++;
  for(auto next_v:G[v]){
    if(seen[next_v])continue;
    dfs(G,next_v,time);
  }
  last[v]=time++;
  }
}
//main関数内で
  seen.assign(n, false); // 全頂点を「未訪問」に初期化
  first.resize(n);
  last.resize(n);
*/
//多次元配列の宣言名
//vector<vector<ll>> field(h, vector<ll>(w));
//記念
/*
field.assign(h, vector<ll>(w, 0));
vector<vector<ll>> field;
void dfs(ll x,ll y,ll h,ll w){
  field[x][y]=0;
  loop(i,8){
    ll next_x=x+dx[i],next_y=y+dy[i];
    if(next_x<0||next_x>=h||next_y<0||next_y>=w)continue;
    if(field[next_x][next_y]==0)continue;
    dfs(next_x,next_y,h,w);
  }
}
*/
template <class T>
inline void chmax(T &a, T b)
{
  if (a < b)
    a = b;
}
template <class T>
inline void chmin(T &a, T b)
{
  if (a > b)
    a = b;
}
int main()
{
  ll q;
  cin >> q;
  while (q--)
  {
    string x;
    string y;
    cin >> x >> y;
    ll dp[x.size() + 1][y.size() + 1]; //dp[a][b]:xのa文字目までとyのb文字目までのLCS
    loop(i, x.size() + 1) loop(j, y.size() + 1) dp[i][j] = 0;
    loop(i, x.size())
    {
      loop(j, y.size())
      {
        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]);
      }
    }
    putout(dp[x.size()][y.size()]);
  }
  return 0;
}

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

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

static const int N = 10000;

int lcs(string X, string Y);

int main(void){
	string s1, s2;
	int n; cin >> n;
	int i;
	REP(i, n) {
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

// longest common sequence
int lcs(string X, string Y){
	int c[N + 1][N + 1];
	int m = X.size();
	int n = Y.size();
	int maxl = 0;
	X = ' ' + X;
	Y = ' ' + Y;
	int i, j;
	FOR(i, 1, m + 1) FOR(j, 1, n + 1) {
		if (X[i] == Y[j]){
			c[i][j] = c[i - 1][j - 1] + 1;
		}else{
			c[i][j] = max(c[i - 1][j], c[i][j - 1]);
		}
		maxl = max(maxl, c[i][j]);
	}
	return maxl;
}