Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <iostream>

//include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <tuple>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s)
{
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}
template <class T>
inline string toString(T x)
{
    ostringstream sout;
    sout << x;
    return sout.str();
}

//math
//-------------------------------------------
template <class T>
inline T sqr(T x) { return x * x; }

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

//repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0, sizeof(a))

//debug
// #define dump(x) cerr << #x << " = " << (x) << endl;
// #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" \
//                       << " " << __FILE__ << endl;

class LCS_Solver
{
    vector<VL> vl;
    string &s1;
    string &s2;

public:
    LCS_Solver(size_t s, string &s1, string &s2)
        : vl(s, VL(s, -1)), s1(s1), s2(s2)
    {
        vl[0][0] = 0;
        REP(i, s)
        {
            vl[0][i] = 0;
            vl[i][0] = 0;
        }
    }

    LL solve_memoize(int s1n, int s2m);
    LL solve_dp(int s1n, int s2m) const;
};

LL LCS_Solver::solve_memoize(int s1n, int s2m)
{
    // cout << s1n << ", " << s2m << endl;
    if (vl[s1n][s2m] >= 0)
    {
        return vl[s1n][s2m];
    }
    else
    {
        LL ans;
        if (s1[s1n - 1] == s2[s2m - 1])
        {
            ans =
                solve_memoize(s1n - 1, s2m - 1) + 1;
            vl[s1n][s2m] = ans;
            return ans;
        }
        else
        {
            ans = max(
                solve_memoize(s1n - 1, s2m),
                solve_memoize(s1n, s2m - 1));
            vl[s1n][s2m] = ans;
            return ans;
        }
    }
}

const size_t MAX_SIZE = 2000;

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

    LL ans;
    REP(_, n)
    {
        string s1, s2;
        cin >> s1;
        cin >> s2;
        // cout << s1.size() << " " << s2.size() << endl;

        ans = LCS_Solver(MAX_SIZE, s1, s2)
                  .solve_memoize(s1.size(), s2.size());
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define  all(x) (x).begin(),(x).end()
typedef long long ll;

int c[1005][1005];

int lcs(string s1, string s2){
    int n = s1.size();
    int m = s2.size();
    s1 = " " + s1;
    s2 = " " + s2;
    for(int i = 0; i <= n; i++) c[i][0] = 0;
    for(int i = 0; i <= n; i++) c[0][i] = 0;

    int ans = 0;
    for (int i1 = 1; i1 <= n; i1++){
        for(int i2 = 1; i2 <= m; i2++){
            if(s1[i1] == s2[i2]){
                c[i1][i2] = c[i1-1][i2-1] + 1;
            }else{
                c[i1][i2] = max(c[i1-1][i2], c[i1][i2-1]);
            }
        }
        ans = max(ans, c[i1][m]);
    }
    return ans;
}

int main(){
    int q;
    cin >> q;
    vector<int> ans(q);
    for(int i = 0; i < q; i++){
        string s1, s2;
        cin >> s1 >> s2;
        ans[i] = lcs(s1, s2);
    }
    rep(i,q) cout << ans[i] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, x, n) for(int i = x; i <= n; i++)
#define rep3(i, x, n) for(int i = x; i >= n; i--)
#define elif else if
#define sp(x) fixed << setprecision(x)
#define pb push_back
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
const ll MOD = 1e9+7;
//const ll MOD = 998244353;
const int inf = (1<<30)-1;
const ll INF = (1LL<<60)-1;
const ld EPS = 1e-10;
template<typename T> bool chmax(T &x, const T &y) {return (x < y)? (x = y, true) : false;};
template<typename T> bool chmin(T &x, const T &y) {return (x > y)? (x = y, true) : false;};

int main(){
    int T;
    cin >> T;
    while(T--){
        string a, b;
        cin >> a >> b;
        int M = sz(a), N = sz(b);
        int dp[M+1][N+1];
        rep(i, M+1) dp[i][0] = 0;
        rep(j, N+1) dp[0][j] = 0;
        rep(i, M){
            rep(j, N){
                if(a[i] == b[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]);
            }
        }
        cout << dp[M][N] << endl;
    }
}