Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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