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<string>
using namespace std;

int main() {
    int q, max_num, now_max, start_pos, target_pos;
    char target;
    string X, Y;

    cin >> q;

    for (int i = 0; i < q; i++) {
        cin >> X >> Y;
        max_num = 0;
        for (int j = 0; j < Y.length(); j++) {
            start_pos = j;
            target = Y[start_pos];
            target_pos = 0;
            now_max = 0;
            while (start_pos < Y.length()) {
                for (int k = target_pos; k < X.length(); k++) {
                    if (target == X[k]) {
                        now_max++;
                        target = Y[start_pos];
                        target_pos = k + 1;
                        break;
                    }
                }
                start_pos++;
                target = Y[start_pos];
            }
            max_num = max(max_num, now_max);
        }
        cout << max_num << endl;
    }
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[1001][1001];
string s,t;
int n,m,a,ans;
int main(){
  cin>>a;
  for(int b=0;b<a;b++){
    cin>>s>>t;
    for(int i=0;i<=1000;i++)
      for(int j=0;j<=1000;j++)
    dp[i][j]=0;
    m=s.size();
    n=t.size();
    ans=0;
    for(int i=1;i<=m;i++){
      for(int j=1;j<=n;j++){
    if(s[i-1]==t[j-1]){
      dp[i][j]=dp[i-1][j-1]+1;
    }else{
      dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    }
      }
    } 
    cout<<dp[m][n]<<endl;
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#define N 1000

int c[N + 1][N + 1];

int max(int x, int y)
{
	return (x > y) ? x : y;
}

int lcs(std::string X, std::string Y)
{
	int ret = 0;
	int m = X.length();
	int n = Y.length();
	X = " " + X;
	Y = " " + Y;
	for (int i = 0; i <= m; i++) c[i][0] = 0;
	for (int j = 1; j <= n; j++) c[0][j] = 0;

	for (int i = 1; i <= m; i++)
	{
		for (int 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][j - 1], c[i - 1][j]);
			}
			ret = max(c[i][j], ret);
		}
	}

	return ret;
}

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

	for (int i = 0; i < n; i++)
	{
		std::string x, y;
		std::cin >> x;
		std::cin >> y;
		std::cout << lcs(x, y) << std::endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const ll LongINF=1e13+7;
const int INF=1e9+7;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};

template<class T> inline bool chmin(T& a, T b) {if (a > b) {a = b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) {a = b;return true;}return false;}
template <class T, class Y>T GCD(T a, Y b){if(a<b){int c=a;a=b;b=c;}if (a % b == 0){return b;}return GCD(b, a % b);}
void clear(queue<pair<int, int>> &q){queue<pair<int, int>> empty;swap(q, empty);} //queueの中身の型は適時変更を忘れない

#define REP(i,a,b) for(int i=(a);i<(b);i++)
#define rep(i,n) REP(i,0,n)
#define pb push_back
#define rv reverse

int main(){
    int N;
    cin>>N;
    rep(c,N){
        string A,B;
        cin>>A>>B;
        int a=A.length();
        int b=B.length();
        vector<vector<int> >dp(a+1,vector<int>(b+1,0));

        //i,j時点での最長文字列を格納する
        //一致した文字があればそこでインクリメントする。(iの時)
        //以前に共通文字列がカウントされていればそれに合わさる形となる。前後関係も正しい(i+1の時)

        rep(i,a){
            rep(j,b){
                if(A[i]==B[j])dp[i+1][j+1]=max(dp[i][j]+1,dp[i+1][j+1]);
                dp[i+1][j+1]=max(dp[i+1][j],dp[i+1][j+1]);
                dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j+1]);
            }
        }
        cout<<dp[a][b]<<endl;
    }

    return 0;
}

//a=97,z=122

C++: #include<bits/stdc++.h>
using namespace std;


int main(int argc, char *argv[]){
  int n;
  cin >> n;
  for (int r = 0; r < n; r++) {

    string x, y;
    cin >> x >> y;
    string dp[y.length() + 1][x.length() + 1];

    for (int i = 0; i < y.length(); i++) {
      for (int j = 0; j < x.length(); j++) {
        if(y[i] == x[j]){
          dp[i + 1][j + 1] = dp[i][j] + y[i];
        }else{
          dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
        }
      }
    }
    std::cout << dp[y.length()][x.length()].length() << std::endl;
  }
  return 0;
}