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 <bits/stdc++.h>
using namespace std;

vector<vector<int> > D(2000, vector<int>(2000, -1));

// Xのi番目以前とYのj番目以前の最長共通部分列の大きさを返す
int lcs(string &X, string &Y, int i, int j)
{
  // メモされているとき
  if (D.at(i).at(j) > -1)
  {
    return D.at(i).at(j);
  }

  // ベースケース
  if (i == 0)
  {
    for (int tmp = 0; tmp <= j; tmp++)
    {
      if (X.at(i) == Y.at(tmp))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  if (j == 0)
  {
    for (int tmp = 0; tmp <= i; tmp++)
    {
      if (X.at(tmp) == Y.at(j))
      {
        D.at(i).at(j) = 1;
        return 1;
      }
    }
    D.at(i).at(j) = 0;
    return 0;
  }
  // 再帰ステップ
  int M = 0;
  if (i > 0)
  {
    M = max(M, lcs(X, Y, i - 1, j));
  }
  if (j > 0)
  {
    M = max(M, lcs(X, Y, i, j - 1));
  }
  if ((i > 0 && j > 0) && (X.at(i)) == Y.at(j))
  {
    M = max(M, lcs(X, Y, i - 1, j - 1) + 1);
  }
  D.at(i).at(j) = M;
  return M;
}

int main()
{
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
  {
    string X, Y;
    cin >> X;
    cin >> Y;
    cout << lcs(X, Y, X.size() - 1, Y.size() - 1) << endl;
    vector<vector<int> > E(2000, vector<int>(2000, -1));
    D = E;
  }
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[y]) {
                    dp[x + 1][y + 1] = dp[x][y] + 1;
                }
                else {
                    if (dp[x + 1][y] > dp[x][y + 1]) {
                        dp[x + 1][y + 1] = dp[x + 1][y];
                    }
                    else {
                        dp[x + 1][y + 1] = dp[x][y + 1];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
    }

    return 0;
}

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

void solve(){
	string a,b;
	cin >> a >> b;
	int dp[1001][1001];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<=a.size();i++){
		for(int j=0;j<=b.size();j++){
			if(i > 0)
				dp[i][j] = max(dp[i][j],dp[i-1][j]);
			if(j > 0)
				dp[i][j] = max(dp[i][j],dp[i][j-1]);
			if(i > 0 && j > 0)
				dp[i][j] = max(dp[i][j],dp[i-1][j-1] + (a[i-1] == b[j-1] ? 1 : 0));
		}
	}
	cout << dp[a.size()][b.size()] << endl;
}
int main(void) {
	int q;
	cin >> q;
	for(int i=0;i<q;i++){
		solve();
	}
	return 0;
}

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

int LCS(const string &a, const string &b) {
	vector<vector<int>> dp(a.size() + 1, vector<int>(b.size() + 1, 0));
	for (int i = 0; i <= a.size(); i++) {
		for (int j = 0; j <= b.size(); j++) {
			if (i > 0 && j > 0) {
				if (a[i - 1] == b[j - 1]) {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
				}
				else {
					dp[i][j] = max(dp[i][j], dp[i - 1][j - 1]);
				}
				dp[i][j] = max(dp[i][j], dp[i][j - 1]);
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
			else if (i > 0) {
				dp[i][j] = max(dp[i][j], dp[i - 1][j]);
			}
			else if (j > 0) {
				dp[i][j] = max(dp[i][j], dp[i][j - 1]);
			}
		}
	}
	return dp[a.size()][b.size()];
}

int main()
{
	int n;
	string X, Y;
	cin >> n;
	while (n--) {
		cin >> X >> Y;
		cout << LCS(X, Y) << endl;
	}
	return 0;
}

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

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

    for (i = 1 ; i <= m ; i++ ) c[i][0] = 0;
    for (j = 1 ; j <= n ; j++ ) c[0][j] = 0;

    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][j-1], c[i-1][j]);
            }
            maxlen = max(maxlen, c[i][j]);
        }
    }
    return maxlen;
}

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
    int q;
    cin >> q;

    int ans[q];

    rep(k, q) {
        string x, y;
        cin >> x >> y;

        int m = x.size();
        int n = y.size();

        int dp[m+1][n+1] = {};  // dp[i][j] : xのi文字目までとyのj文字目までのLCSの長さ(i, jは0-index)

        rep(i, m) {
            rep(j, n) {
                if(x[i] == y[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]);
                }
            }
        }

        ans[k] = dp[m][n];
    }

    rep(i, q) {
        cout << ans[i] << "\n";
    }

    return 0;
}