Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	n := scanInt(sc)
	results := []int{}
	for i := 0; i < n; i++ {
		results = append(results, resolveInstance())
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func resolveInstance() int {
	x, y := scanString(sc), scanString(sc)
	lx, ly := len(x), len(y)
	dp := make([][]int, lx+1)
	dp[0] = make([]int, ly+1)
	for i := 1; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
		dp[i][0] = dp[i-1][0]
		for j := 1; j <= ly; j++ {
			dp[i][j] = dp[i-1][j]
			if dp[i][j] < dp[i][j-1] {
				dp[i][j] = dp[i][j-1]
			}
			if x[i-1] == y[j-1] {
				cL := dp[i-1][j-1] + 1
				if dp[i][j] < cL {
					dp[i][j] = cL
				}
			}
		}
	}
	return dp[lx][ly]
}

const (
	remove = 0 + iota
	match
)

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

#define SIZE 1002
int FTable[SIZE][SIZE];
class TLcsSolver
{
    const char* FArray1;
    const char* FArray2;
    const int Fn1;
    const int Fn2;

    int Count(int i, int j)
    {
        if(i == 0 || j == 0) return 0;
        if(FArray1[i-1] == FArray2[j-1]){
            return FTable[i-1][j-1] + 1;
        }
        return std::max(FTable[i-1][j], FTable[i][j-1]);
    }
    void Solve()
    {
        for(std::size_t i = 1; i < Fn1; ++i){
            for(std::size_t j = 1; j < Fn2; ++j){
                FTable[i][j] = Count(i, j);
            }
        }
    }
    void Print()const
    {
        for(std::size_t i = 0; i < Fn1; ++i){
            for(std::size_t j = 0; j < Fn2; ++j){
                printf("%d\t", FTable[i][j]);
            }
            printf("\n");
        }
    } 
public:
    TLcsSolver(const char* a, const char* b)
     : FArray1(a), FArray2(b)
     , Fn1(std::strlen(a)), Fn2(std::strlen(b))
    {
        Solve();
        // Print();
    }
    int Answer()const
    {
        return FTable[Fn1-1][Fn2-1];
    }
};

int main()
{
    int n;
    std::scanf("%d\n", &n);
    char a[SIZE];
    char b[SIZE];
    for(int i = 0; i < n; ++i){
        std::fgets(a, SIZE, stdin);
        std::fgets(b, SIZE, stdin);
        std::printf("%d\n", TLcsSolver(a, b).Answer());
    }
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
 
#define MAX 1010
#define max(a,b) ((a<b)?b:a)
 
char s[MAX], t[MAX];
int dp[MAX][MAX],path[MAX][MAX];
 
int LCS() {
  int n = strlen(s);
  int m = strlen(t);
  memset(dp,0,sizeof dp);
  int i,j,maxi=0;
  for(i=0;i<n;i++) {
    for(j=0;j<m;j++) {
      if( i ) dp[i][j] = max(dp[i][j],dp[i-1][j]);
      if( j ) dp[i][j] = max(dp[i][j],dp[i][j-1]);
      if( s[i] == t[j] ) {
    dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j]+1);
    maxi = max(maxi,dp[i+1][j+1]);
      }
    }
  }
  return maxi;
}
 
int main() {
  int n,i;
  scanf("%d",&n);
  for(i=0;i<n;i++) {
  scanf(" %s",&s[0]);
  scanf(" %s",&t[0]);
  printf("%d\n",LCS());
  }
  return 0;
}

C++: n=int(input())
ans=[]
for k in range(n):
    s1=input()
    s2=input()
    dp=[[0 for i in range(len(s2)+1)] for j in range(len(s1)+1)]
    for i in range(1,len(s1)+1):
        for j in range(1,len(s2)+1):
            if s1[i-1]==s2[j-1]:
                dp[i][j]=dp[i-1][j-1]+1
            else:
                dp[i][j]=max(dp[i-1][j],dp[i][j-1])


    ans.append(dp[len(s1)][len(s2)])
for a in ans:
    print(a)

C++: #ifdef BUILT_LOCAL

#include "niklib.h"

#endif

#include <iostream>
#include <utility>
#include <numeric>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <set>
#include <list>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
constexpr ll mod = 1000000007;

typedef long long num;

#define bs(n) (1ll<<(ll)(n))
#define rep(i, n) for(ll (i) = 0;(i) < (n);(i)++)
#define rrep(i, n) for(ll (i) = 1;(i) <= (n);(i)++)

int v[1010][1010], f[1010][1010][2];

int lcs2(string &a, string &b) {
    memset(v, 0, sizeof(int[1010][1010]));
    memset(f, 0, sizeof(int[1010][1010][2]));
    int n = a.length(), m = b.length(), ri = 0, rj = 0;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            if (a[i] == b[j] && v[i + 1][j + 1] < v[i][j] + 1) {
                v[i + 1][j + 1] = v[i][j] + 1;
                f[i + 1][j + 1][0] = i;
                f[i + 1][j + 1][1] = j;
            }
            if (v[i + 1][j] < v[i][j]) {
                v[i + 1][j] = v[i][j];
                f[i + 1][j][0] = i;
                f[i + 1][j][1] = j;
            }
            if (v[i][j + 1] < v[i][j]) {
                v[i][j + 1] = v[i][j];
                f[i][j + 1][0] = i;
                f[i][j + 1][1] = j;
            }
        }
    }
    for (int i = 0; i <= n; i++)
        for (int j = 0; j <= m; j++)
            if (v[ri][rj] < v[i][j]) {
                ri = i;
                rj = j;
            }
    string ret;
    int rn = v[ri][rj];
    while (ri != 0 && rj != 0) {
        if (f[ri][rj][0] == ri - 1 && f[ri][rj][1] == rj - 1) {
            ret.push_back(a[ri]);
        }
        int rii = f[ri][rj][0];
        int rjj = f[ri][rj][1];
        ri = rii;
        rj = rjj;
    }
    return rn;
}


int main() {
    int q;
    cin >> q;
    string a, b;
    rep(i, q) {
        cin >> a >> b;
        cout << lcs2(a, b) << endl;
    }
}