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 <bits/stdc++.h>

using namespace std;

int solve(string &s, string &t, vector<vector<int>> &memo, int i = 0,
          int j = 0) {
  if (i >= s.size() || j >= t.size()) {
    return 0;
  }

  if (memo[i][j] < 0) {
    if (*(s.end() - i - 1) == *(t.end() - j - 1)) {
      memo[i][j] = solve(s, t, memo, i + 1, j + 1) + 1;
    } else {
      memo[i][j] =
          max(solve(s, t, memo, i + 1, j), solve(s, t, memo, i, j + 1));
    }
  }

  return memo[i][j];
}
int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string s, t;
    cin >> s >> t;
    vector<vector<int>> memo(s.size(), vector<int>(t.size(), -1));
    cout << solve(s, t, memo) << '\n';
  }

  return 0;
}

C++: #include<cstdio>
#include<cstring>
#include<algorithm>

using namespace std;

int map[2000][2000];

int main(void) {
    int q;
    scanf("%d",&q);
    for(int i=0; i<q; i++) {
        char x[2000], y[2000];
        scanf("%s%s",x,y);
        int xl=strlen(x),yl=strlen(y);

        for(int m=0; m<xl; m++)
            map[m][0] = 0;
        for(int n=0; n<yl; n++)
            map[0][n] = 0;
        for(int m=1; m<=xl; m++)
            for(int n=1; n<=yl; n++) {
                if(x[m] == y[n])
                    map[m][n] = max(max(map[m-1][n-1]+1, map[m-1][n]), map[m][n-1]);
                else
                    map[m][n] = max(max(map[m-1][n-1]+0, map[m-1][n]), map[m][n-1]);
            }
        printf("%d\n", map[xl][yl]);
    }
    return 0;
}

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

constexpr int MAX_XY = 1000;

int lx, ly, q, dp[MAX_XY][MAX_XY];
std::string sx, sy;

void fill_dp(){
    for(int i = 1; i <= lx; ++i){
        for(int j = 1; j <= ly; ++j){
            if(sx[i - 1] == sy[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
}

void solve(){
    fill_dp();
    std::cout << dp[lx][ly] << std::endl;
    return;
}

int main(){
    std::cin >> q;
    
    for(int i = 0; i < q; ++i){
        std::cin >> sx >> sy;
        lx = sx.size(), ly = sy.size();
        solve();
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPP(i, n) for(int i = 1; i <= n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
using namespace std;
typedef long long ll;
typedef pair<ll, ll> p;
const ll MOD = 1000000007;

ll lcs(string s, string t) {
    int n = s.length();
    int m = t.length();
    vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
    dp[0][0] = 0;
    REPP(i,n) {
        REPP(j,m) {
            if(s[i-1]==t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
            else dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
        }
    }
    return dp[n][m];
}

int main() {
    int q; cin >> q;
    REP(i,q) {
        string S,T; cin >> S >> T;
        cout << lcs(S,T) << endl;
    }
    return 0;
}