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>
#include<algorithm>
using namespace std;
static const int N = 1000;

int lcs(string X, string Y) {
  int c[N + 1][N + 1];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for ( int i = 1; 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-1][j] , c[i][j-1]);
      }
      maxl = max(maxl, c[i][j]);
    }
  }

  return maxl;
}

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

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

using namespace std;
static const int N=1000;
int lcs(string X, string Y){
    int m=X.size();
    int n=Y.size();
    int c[N+1][N+1];
    int maxl=0;
    X=' '+X;
    Y=' '+Y;
    int i,j;
    for(i=1;i<m;i++)c[i][0]=0;
    for(j=1;j<n;j++)c[0][j]=0;

    for(i=1;i<m;i++){
        for(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]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main()
{
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    
    return 0;
}

C++: #include<math.h>
#include<limits.h>
#include<iostream>
#include<string>
#include<vector>
#include<stdio.h>
#include<sstream>
#include<list>
#include<queue>
#include<algorithm>
#include<functional>
#include<map>
#include<set>
//#include<utility>
//#include<initializer_list>
//#include<tuple>
//#include<regex>
using namespace std;
#define ull unsigned long long
#define ll long long
#define rep(i, n) for(int i=0;i<(int)(n);i++)
#define all(t) t.begin(), t.end()
bool isSubstring(string substr, string t) {
	int n = substr.length();
	int seek = 0;
	rep(i, n) {
		bool ex = 0;
		for (int j = seek; j < t.length(); j++) {
			if (substr[i] == t[j]) {
				ex = 1;
				seek = j + 1;
				break;
			}
		}
		if (ex) {
			continue;
		}
		else {
			return false;
		}
	}
	return true;

}

int LongestCommonSubstring(string s, string t) {
	// Wrong plan
	vector<int>dp(s.length(), 0);
	int n = s.length();
	string lcs = "";
	if (isSubstring(s.substr(n - 1, n), t)) {

		dp[n - 1] = 1;
		lcs = s[n - 1];
	}
	for (int i = n - 2; i >= 0; i--) {
		//string substr = s.substr(i, n);
		string substr = s[i] + lcs;
		int issubst = isSubstring(substr, t);
		if (issubst) lcs = substr;
		dp[i] = dp[i + 1] + issubst;
	}
	return dp[0];
}
int LongestCommonSubstring2(string s, string t) {
	vector<vector<int>> dp(s.length(), vector<int>(t.length(), 0));
	int n = s.length();
	int m = t.length();
	rep(i, n - 1) {
		rep(j, m - 1) {
			dp[i + 1][m + 1] = s[i] == t[j] ? dp[i][j] + 1 : max(dp[i][j + 1], dp[i + 1][j]);
		}
	}
	return dp[n - 1][m - 1];
}
int main() {
	//cout << isSubstring("abc", "abcde") << endl;
	//cout << isSubstring("bd", "abcde") << endl;
	//cout << isSubstring("db", "abcde") << endl;

	//cout << LongestCommonSubstring("bacd", "becd") << endl;
	//cout << LongestCommonSubstring("abcd", "becd") << endl;
	//cout << endl;
	//cout << LongestCommonSubstring("a", "a") << endl;
	//cout << LongestCommonSubstring("bbb", "bbb") << endl;
	//cout << LongestCommonSubstring("aba", "bbaba") << endl;
	//cout << LongestCommonSubstring("abababbbaa", "bbabaabba") << endl;
	//cout << LongestCommonSubstring("a", "aaaaa") << endl;
	//cout << LongestCommonSubstring("aizojfeiz", "aaaizojifeizz") << endl;
	//cout << LongestCommonSubstring("zzyiioxizu", "zzsyoizuo") << endl;
	//cout << LongestCommonSubstring("iooooioiooiio", "ooiiioioiooooiioio") << endl;
	//cout << LongestCommonSubstring("ajfoaooieellzljieoaelj", "ajoiiffoaolliseellzljieoaelj") << endl;
	//cout << LongestCommonSubstring("zzzzzyyzyy", "zzyyyyz") << endl;
	//cout << LongestCommonSubstring("abcdef", "fedcba") << endl;
	//cout << LongestCommonSubstring("abc", "xyz") << endl;
	int q;
	cin >> q;
	vector<string> s(q);
	vector<string> t(q);
	rep(i, q) {
		cin >> s[i] >> t[i];
	}
	rep(i, q) {
		cout << LongestCommonSubstring2(s[i], t[i]) << endl;
	}

	return 0;
}

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

using namespace std;

int main() {

	int n;
	string A, B;
	int sizA, sizB, max=0;
	int c[1001][1001];
	
	cin >> n;
	
	for (int i = 0;i < n;++i) {
		cin >> A >> B;
		sizA = (int)(A.length());
		sizB = (int)(B.length());

		for (int a = 0;a <= sizA;++a) {
			for (int b = 0;b <= sizB;++b) {
				if (a == 0 || b == 0) {
					c[a][b] = 0;
				}else if(A[a-1]==B[b-1]){
					c[a][b] = c[a-1][b-1]+1;
				}else if(c[a][b-1]>c[a-1][b]){
				    c[a][b] = c[a][b-1];
				}else{
				    c[a][b] = c[a-1][b];
				}



				if (max < c[a][b]) {
					max = c[a][b];
				}
			}
		}
		cout << max << endl;
		max = 0;
	}
	return 0;
}