Go: package main

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

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for j := 1; j <= n; j++ {
			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
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: #include<iostream>
#include<string>
#include<algorithm>
using namespace std;
static const int N = 1000;

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;
    for (int i = 0; i <= m; i++){
        c[i][0] =0;
    }
    for (int i= 0; i <= n; i++){
        c[0][i] = 0;
    }

    for (int i = 1;i <= m; i++){
        for(int j = 1; j <= n;j++){
            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;
}

int main(){
    string s1,s2;
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        cin >> s1 >> s2;
        cout << lcs(s1,s2) <<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 s[2001],c[2001];
cin>>q;
for(e=0;e<q;e++){
scanf("%s %s",s,c);
l=strlen(s);
z=strlen(c);

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(c[i-1]==s[j-1])p[j][i]=p[j-1][i-1]+1;
		else p[j][i]=max(p[j][i-1],p[j][i-1]);
	}
}
cout<<p[l][z]<<endl;
}
return 0;
}

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;
    }
}