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<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string a;
string b;
int ans[1000+1][1000+1];
int lcs()
{
    int al=a.size();
    int bl=b.size();
    int maxn=-1;
    a=" "+a;
    b=" "+b;
    for (int i=1;i<=al;i++)
    {
        for (int j=1;j<=bl;j++)
        {
            if(a[i]==b[j])
            {
                ans[i][j]=ans[i-1][j-1]+1;
            }
            else {
                ans[i][j]=max(ans[i-1][j],ans[i][j-1]);
            }
            if(ans[i][j]>maxn)
                maxn=ans[i][j];
        }
    }
   return maxn;
}
int main()
{
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        cin>>a;
        cin>>b;
        int sum=lcs();
        cout<<sum<<endl;
    }
    return 0;
}

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

int main(){
  int q;
  cin >> q;
  string X,Y;

  for(int i=0;i<q;i++){
    cin >> X >> Y;
    int dp[1001][1001]={};
    for(int j=1;j<=Y.size();j++){
      for(int k=1;k<=X.size();k++){
	if(X[k-1]==Y[j-1]) dp[j][k]=dp[j-1][k-1]+1;
	else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
      }
    }
    cout << dp[Y.size()][X.size()] << endl;
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;


//????????±?????¨??????
const int MAX_N = 1000;
int dp[MAX_N + 1][MAX_N + 1];

int solve(string s, string t, int n, int m){
    rep(i,n){
        rep(j,m){
            if(s[i] == t[j]){
                dp[i + 1][j + 1] = dp[i][j] + 1;
            }else{
                dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
            }
        }
    }
    return dp[n][m];
}

int main(){
    int n;
    string s, t;
    cin >> n;
    rep(i,n){
        cin >> s >> t;
        cout << solve(s, t, s.size(), t.size()) << endl;
    }
}

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

int dp[1000+1][1000+1];

int main() {
    int n; cin >> n;
    while(n--) {
        string s, t; cin >> s >> t;
        int N = s.length();
        int M = t.length();
        dp[0][0] = 0;
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; 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[N][M] << endl;
    }
}