Go: package main

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

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <iostream>
#include <cmath>
#include <set>
#include <string>
#include <algorithm>
#include <vector>
#include <bitset>
#include <iomanip>
#include <map>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define reps(i,s,e) for(int i=s; i<(int)e; i++)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Graph = vector<vector<T>>;

void printFixedDouble(double d, int fix) {
    cout << std::fixed;
    cout << std::setprecision(fix) << d << endl;
}

int main() {
    int datanum;
    cin >> datanum;
    rep(d, datanum) {
        string a, b;
        cin >> a >> b;

        int aLen = a.length();
        int bLen = b.length();

        int dp[aLen + 1][bLen + 1];
        rep(i, aLen + 1) {
            dp[i][0] = 0;
        }
        rep(i, bLen + 1) {
            dp[0][i] = 0;
        }

        rep(i, aLen) {
            rep(j, bLen) {
                if (a[i] == b[j]) {
                    dp[i+1][j+1] = std::max(std::max(dp[i+1][j], dp[i][j+1]), dp[i][j] + 1);
                } else {
                    dp[i+1][j+1] = std::max(dp[i+1][j], dp[i][j+1]);
                }
            }
        }

        cout << dp[aLen][bLen] << endl;
    }
}

C++: #include <iostream>
#include <string>

using namespace std;
const int L = 1001;
const int INF = -1001001001;

int main()
{
    string s, t;
    int dp[L][L];
    int q;
    cin >> q;
    for(int i=0;i<q;++i)
    {
        cin >> s;
        cin >> t;
        for(int j=0;j<s.size()+1;++j) dp[j][0] = 0;
        for(int j=0;j<t.size()+1;++j) dp[0][j] = 0;
        for(int j=1;j<s.size()+1;++j)
        {
            for(int k=1;k<t.size()+1;++k)
            {
                if(s[j-1] == t[k-1])
                {
                    dp[j][k] = max(max(dp[j][k-1], dp[j-1][k-1] + 1), dp[j-1][k]);
                }
                else
                {
                    dp[j][k] = max(dp[j][k-1], dp[j-1][k]);
                }
            }
        }
        cout << dp[s.size()][t.size()] << endl;
    }
}

C++: #include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define MAX_q 150
#define MAX_L 1000
string X, Y;
int dp[MAX_L + 1][MAX_L + 1];
void DP() {
    rep(i, X.size()) {
        rep(k, Y.size()) { dp[i][k] = 0; }
    }
    rep(i, X.size()) {
        rep(k, Y.size()) {
            if (X[i] == Y[k]) {
                dp[i + 1][k + 1] = dp[i][k] + 1;
            } else {
                dp[i + 1][k + 1] = max(dp[i][k + 1], dp[i + 1][k]);
            }
        }
    }
}
int main() {
    int q;
    cin >> q;
    rep(j, q) {
        cin >> X >> Y;
        DP();
        cout << dp[X.size()][Y.size()] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[1001][1001];




	
int main (){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	//input
	int n;
	cin >> n;
	vector<string> S(n);
	vector<string> T(n);
	for(int i=0;i<n;i++)cin >> S[i] >> T[i];


	//alorithm
	for(int i=0;i<n;i++){
		string s,t;
		s=S[i];
		t=T[i];
		memset(dp,0,sizeof(dp));
		for(int i=1;i<=s.size();i++){
			for(int j=1;j<=t.size();j++){
				if(s[i-1]==t[j-1]){dp[i][j]=dp[i-1][j-1]+1;continue;}
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout << dp[s.size()][t.size()] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;

void print() { cout << endl; }
template <typename Head, typename... Tail>
void print(Head head, Tail... tail) {
    int size = sizeof...(Tail);
    cout << head;
    if (size > 0) {
        cout << " ";
    }
    print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
    cout << head;
    print0(tail...);
}

struct xstruct {
    ll val;
};
bool operator<=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val <= rhs.val;
}
bool operator<(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val < rhs.val;
}
bool operator>=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val >= rhs.val;
}
bool operator>(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val > rhs.val;
}
bool operator==(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val == rhs.val;
}
bool operator!=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val != rhs.val;
}
ostream& operator<<(ostream& os, xstruct const& xst) {
    return os << xst.val;
}

ll recursive(ll a, ll b, string& sa, string& sb, vector<vector<ll>>& dp) {
    ll maxval = 0;
    if (a == sa.length() || b == sb.length()) {
        return maxval;
    }
    if (dp[a][b] >= 0) {
        return dp[a][b];
    }

    if (sa.at(a) == sb.at(b)) {
        maxval = max(maxval, 1 + recursive(a + 1, b + 1, sa, sb, dp));
    }
    maxval = max(maxval, recursive(a + 1, b, sa, sb, dp));
    maxval = max(maxval, recursive(a, b + 1, sa, sb, dp));
    dp[a][b] = maxval;
    return dp[a][b];
}

int main() {
    ll N = 0;
    cin >> N;
    string nil;
    getline(cin, nil);
    for (ll iter = 0; iter < N; iter++) {
        string sa, sb;
        getline(cin, sa);
        getline(cin, sb);
        ll la = sa.length();
        ll lb = sb.length();

        vector<vector<ll>> dp(la, vector<ll>(lb));
        for (ll a = 0; a < la; a++) {
            for (ll b = 0; b < lb; b++) {
                dp[a][b] = -1;
            }
        }
        recursive(0, 0, sa, sb, dp);
        print(dp[0][0]);
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <numeric>
#include <climits>
using namespace std;
//using ll = long long;
//const ll mod = 1000000007;
int n, m;
string s, t;
const int n_max = 1000;
const int m_max = 1000;

int main() {
	cin >> n;
	for(int k=0; k<n; ++k){
        cin >> s >> t;
        int dp[n_max+1][m_max+1] = {0};
	for (int i = 0; i < s.size(); ++i) {
		for (int j = 0; j < t.size(); ++j) {
			if (s[i] == t[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[s.size()][t.size()] << endl;
    }
	

	return 0;
}