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>

#define debug(x) cout <<"DEBUG"<<" "<< #x << ":" << x << '\n'
#define rep(i,n) for(int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++)   // 0-indexed up
#define rep1(i,n) for(int_fast32_t i = 1; i <= ((int_fast32_t)(n)); i++) // 1-indexed up
#define rrep(i,n) for(int_fast32_t i = ((int_fast32_t)(n)-1); i >= 0; i--)  // 0-indexed down
#define rrep1(i,n) for(int_fast32_t i = ((int_fast32_t)(n)); i >= 1; i--)   // 1-indexed down
#define all(x) (x).begin(),(x).end()

using namespace std;

template<typename T>
using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>;
using PII = pair<int_fast32_t, int_fast32_t>;

template<class T>
using maxheap = std::priority_queue<T>;
template<class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template<class T,class U> inline bool chmax(T &a, const U &b){if(a<b){a=b;return 1;}return 0;}
template<class T,class U> inline bool chmin(T &a, const U &b){if(a>b){a=b;return 1;}return 0;}

const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL<<62)-1;
constexpr int inf = (1<<30)-1;
const int mod = 1000000007;
// const int mod = 998244353;


/*FUNCs=================================================*/


/*MAIN==================================================*/
signed main(){
    double START_TIME = clock();
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
//    cout << fixed << setprecision(15);

    i32 q; cin >> q;
    string s, t;
    vii dp;

    rep(i,q) {
        cin >> s >> t;
        dp.assign(s.size()+1, vi(t.size()+1));
        rep(i, s.size()) rep(j, t.size()) {
            if(s[i] == t[j]) chmax(dp[i+1][j+1], dp[i][j]+1);
            chmax(dp[i+1][j+1], dp[i+1][j]);
            chmax(dp[i+1][j+1], dp[i][j+1]);
        }
        
        // debug
//        rep(i,s.size()+1)rep(j, t.size()+1) cout << dp[i][j] << (j == t.size() ? '\n' : ' ');

        cout << dp[s.size()][t.size()] << '\n';
    }

//     printf("ElapsedTime: %5.3f ms\n",1000*((clock()-START_TIME)/CLOCKS_PER_SEC));
}

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

using namespace std;

static const int N = 1000;
static int c[N + 1][N + 1] = {};

static int lcs(string& X, string& Y) {
  const size_t m(X.size());
  const size_t n(Y.size());

  int lcs = 0;
  
  for (size_t i = 1; i <= m; i++) {
    for (size_t j = 1; j <= n; j++) {
      if (X[i] == Y[j]) {
        c[i][j] = c[i - 1][j - 1] + 1;
      }
      else {
        c[i][j] = max(c[i - 1][j], c[i][j - 1]);
      }
      lcs = max(lcs, c[i][j]);
    }
  }
  return lcs;
}

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

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

C++: // -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_10_C: Longest Common Subsequence

#include <algorithm>
#include <cstdio>
#include <cstring>

static unsigned int lcs(const char* x, const char* y)
{
  auto m = static_cast<int>(std::strlen(x));
  auto n = static_cast<int>(std::strlen(y));
  unsigned int c0[m+1][n+1]; // extend -1
  auto const c = (c0+1)+1;

  for (auto i = -1; i < m; ++i) c[i][-1] = 0;
  for (auto j = 0; j < n; ++j) c[-1][j] = 0;

  for (auto i = 0; i < m; ++i) {
    for (auto j = 0; j < n; ++j) {
      c[i][j] = (x[i] == y[j])
        ? c[i-1][j-1] + 1
        : std::max(c[i][j-1], c[i-1][j]);
    }
  }
  return c[m-1][n-1];
}

int main()
{
  unsigned int q;
  std::scanf("%u", &q);
  const unsigned int MAX_LEN = 1000;
  char x[MAX_LEN+1], y[MAX_LEN+1];
  for (auto i = 0u; i < q; ++i) {
    std::scanf("%1000s", x);
    std::scanf("%1000s", y);
    std::printf("%u\n", lcs(x, y));
  }
}

// eof

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

int lcs(string X, string Y) {
  int dp[1000 + 1][1000 + 1];
  int max = 0;
  int m = X.size;
  int n = Y.size();
  X = ' ' + X;
  Y = ' ' + Y;
  for (unsigned int i = 1; i <= m; i++)
    dp[i][0] = 0;

  for (unsigned int i = 1; i <= n; i++)
    dp[0][i] = 0;
  for (unsigned int i = 1; i <= m; i++) {
    for (unsigned int j = 1; j <= n; j++) {
      if (X[i] == Y[i])
	dp[i][j] = dp[i - 1][j - 1] + 1;
      else if (dp[i - 1][j] >= dp[i][j - 1])
	dp[i][j] = dp[i - 1][j];
      else
	dp[i][j] = dp[i][j - 1];

      max = (max < dp[i][j]) ? dp[i][j] : max;
    }
  }

  return max;
}

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    string X, Y;
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define len(str) (int)(str.length())
int main() {
	int N;
	cin >> N;

	for (int a = 0; a < N; a++)
	{
		string X, Y;
		cin >> X >> Y;
		vector<vector<int> > dp(len(X) + 1, vector<int>(len(Y) + 1, 0));
		for (int a = 1; a <= len(X); a++)
		{
			for (int b = 1; b <= len(Y); b++)
			{
				if (X[a - 1] == Y[b - 1]) {
					dp[a][b] = dp[a - 1][b - 1] + 1;
				}
				else {
					dp[a][b] = max(dp[a - 1][b], dp[a][b - 1]);
				}
			}
		}
		cout << dp[len(X)][len(Y)] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;

ll dp[1111][1111];

int main() {
    int q;
    cin >> q;
    while (q--) {
        string x, y;
        cin >> x >> y;
        int n = x.size(), m = y.size();
        for (int i = 0; i < 1111; i++) {
            for (int j = 0; j < 1111; j++) {
                dp[i][j] = 0;
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (x[i] == y[j]) dp[i + 1][j + 1] = max(dp[i + 1][j + 1], dp[i][j] + 1);
                dp[i + 1][j + 1] = max({ dp[i + 1][j + 1], dp[i + 1][j], dp[i][j + 1] });
            }
        }
        cout << dp[n][m] << endl;
    }
}