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<iostream>
#include<stack>
#include<vector>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#define _USE_MATH_DEFINES // M_PI????????????????????????
#include<math.h>
#pragma warning(disable:4996)
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; // X[0]?????????????????\
	Y = ' ' + Y; // Y[0]?????????????????\

	for(int i=1; i<=m; i++){
		for(int j=1; j<=n; j++){
			if( X[i]==Y[i] ){
				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) << "\n";
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long
#define rep(i, n) for(int i=0;i<(n);++i)
#define per(i, n) for(int i=(n)-1;i>=0;--i)
#define repa(i, n) for(int i=1;i<(n);++i)
#define foreach(i, n) for(auto &i:(n))
#define pii pair<int, int>
#define pll pair<long long, long long>
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
const ll MOD = (ll)1e9+7;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
using namespace std;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}

ll modpow(ll x, ll b){
	ll res = 1;
	while(b){
		if(b&1)res = res * x % MOD;
		x = x * x % MOD;
		b>>=1;
	}
	return res;
}

ll modinv(ll x){
	return modpow(x, MOD-2);
}

string str1;
string str2;
int len1;
int len2;

vvector<int> dp;

int func(int p1, int p2){
	if(p1==len1||p2==len2)return 0;
	if(dp[p1][p2]>=0)return dp[p1][p2];
	int res = 0;
	if(str1[p1]==str2[p2])res = 1 + func(p1+1, p2+1);
	chmax(res, func(p1+1, p2));
	chmax(res, func(p1, p2+1));
	return dp[p1][p2] = res;
}

int main(){
	int n;
	cin >> n;
	rep(i, n){
		cin >> str1 >> str2;
		len1 = str1.size();
		len2 = str2.size();
		dp.assign(len1, vector<int>(len2, -1));
		cout << func(0, 0) << endl;
	}
	return 0;
}

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

int main()
{
	int n;
	string a, b;

	while (n--)
	{
		int memo1[1005] = {};
		int memo2[1005] = {};
		cin >> a >> b;

		for (int i = 1; i <= a.size(); i++)
		{
			memo2[0] = 0;
			for (int j = 1; j <= b.size(); j++)
			{
				if (a[i] == b[j])
					memo2[j] = max({ memo2[j - 1], memo1[j], memo1[j - 1] + 1 });
				else
					memo2[j] = max(memo2[j - 1], memo1[j]);
			}
			swap(memo1, memo2);
		}

		cout << memo1[b.size()] << endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <climits>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <map>
#include <unordered_map>
#include <set>
#include <unordered_set>
#include <sstream>
#include <numeric>
// #include <boost/multiprecision/cpp_dec_float.hpp>
// #include <boost/multiprecision/cpp_int.hpp>

///////////////////////////////////////////////////////////////
// namespace
///////////////////////////////////////////////////////////////
using namespace std;
// namespace mp = boost::multiprecision;

///////////////////////////////////////////////////////////////
// types
///////////////////////////////////////////////////////////////
using pii = pair<int, int>;
using ll = long long int;
using ld = long double;
// using mpi = mp::cpp_int;
// using mpf = mp::number<mp::cpp_dec_float<1024>>;

///////////////////////////////////////////////////////////////
// mod https://qiita.com/drken/items/3b4fdf0a78e7a138cd9a
///////////////////////////////////////////////////////////////
// ll MOD = 1000000007;
// ll mod(long long val) {
//     long long res = val % MOD;
//     if (res < 0) res += MOD;
//     return res;
// }
// ll modinv(ll a) {
//     ll b = MOD, u = 1, v = 0;
//     while (b) {
//         ll t = a / b;
//         a -= t * b; swap(a, b);
//         u -= t * v; swap(u, v);
//     }
//     u %= MOD;
//     if (u < 0) u += MOD;
//     return u;
// }

vector<vector<int>> dp;

int lcs(const string &x, const string &y, int i, int j) {
    if (i >= x.size() || j >= y.size()) return 0;
    if (dp[i][j] >= 0) return dp[i][j];
    int ret = 0;
    ret = max(ret, lcs(x, y, i + 1, j));
    ret = max(ret, lcs(x, y, i, j + 1));
    if (x[i] == y[j]) ret = max(ret, lcs(x, y, i + 1, j + 1) + 1);
    return dp[i][j] = ret;
}

int lcs(const string &x, const string &y) {
    for (int i = x.size() - 1; i >= 0; --i) {
        for (int j = y.size() - 1; j >= 0; --j) {
            dp[i][j] = max(dp[i][j], dp[i + 1][j]);
            dp[i][j] = max(dp[i][j], dp[i][j + 1]);
            if (x[i] == y[j]) dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1);
        }
    }
    return dp[0][0];
    // return lcs(x, y, 0, 0);
}

int main() {
    int n;
    string x, y;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> x >> y;
        dp = vector<vector<int>>(x.size() + 5, vector<int>(y.size() + 5, 0));
        cout << lcs(x, y) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
#define MAX 1001

int lcs(string X, string Y){
  int c[MAX][MAX]; 
  int m = X.length();
  int n = Y.length();
  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-1] == Y[j-1]){
        c[i][j] = c[i-1][j-1] + 1;
      }else{
        c[i][j] = (c[i][j-1] > c[i-1][j] ? c[i][j-1] : c[i-1][j]);
      }
    }
  }
  return c[m][n];
}

int main(void){
  int q, l;
  string X, Y;
  cin >> q;
  for(int i=0; i<q; i++){
    cin >> X >> Y;
    l = lcs(X, Y);
    cout << l << endl;
  }
  return 0;
}

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

int main(){
  int n; cin >> n;
  for( int i = 0; i < n; i++ ){
    int dp[1010][1010];
    for( int i = 0; i < 1010; i++ ) dp[i][0] = dp[0][i] = 0;
    
    char x[1010], y[1010];
    cin >> x >> y;

    for( int i = 0; i < strlen( x ); i++ ){
      for( int j = 0; j < strlen( y ); j++ ){
	if( x[i] == y[j] ){
	  dp[i + 1][j + 1] = dp[i][j] + 1; 
	}else if( dp[i][j + 1] >= dp[i + 1][j] ){
	  dp[i + 1][j + 1] = dp[i][j + 1];
	}else{
	  dp[i + 1][j + 1] = dp[i + 1][j];
	}
      }
    }

    cout << dp[strlen( x )][strlen( y )] << endl;
  }
}