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 REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, a, b) for (int i = a; i < b; ++i)
#define FORR(i, a, b) for (int i = b - 1; i >= a; --i)
#define SORT(v) sort(v.begin(), v.end())
#define SORTR(v) sort(v.rbegin(), v.rend())
#define REV(v) reverse(v.begin(), v.end())
#define ITER(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define LB(v, x) (lower_bound(v.begin(), v.end(), x) - v.begin())
#define UB(v, x) (upper_bound(v.begin(), v.end(), x) - v.begin())
#define SZ(v) (int)v.size()
using namespace std;
using ll = long long;
using P = pair<int, int>;

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int q;
	cin >> q;
	REP(k, q) {
		string X, Y;
		cin >> X >> Y;
		int x = X.size(), y = Y.size();
		vector<vector<int>> dp(x + 1, vector<int>(y + 1));
		REP(i, x) {
			REP(j, y) {
				if (X[i] != Y[j]) {
					dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
				} else {
					dp[i + 1][j + 1] = dp[i][j] + 1;
				}
			}
		}
		cout << dp[x][y] << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;
typedef pair<ll,ll> pll;
const ll mod=1e9+7;
//const ll mod=998244353;
const ll inf=5e18;

int main() {
  ll q;
  cin >> q;
  while(q--) {
    string s;
    string t;
    cin >> s >> t;
    mat dp(s.size()+1,vec(t.size()+1));
    for(ll i=0;i<s.size();i++) {
      for(ll j=0;j<t.size();j++) {
        if(s[i]==t[j]) {
          dp[i+1][j+1]=max({dp[i][j]+1,dp[i+1][j],dp[i][j+1]});
        }
        else {
          dp[i+1][j+1]=max({dp[i][j],dp[i+1][j],dp[i][j+1]});
        }
      }
    }
    cout << dp[s.size()][t.size()] << endl;
  }
}

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

int Max(int &a,int b){return a=max(a,b);}

int LCS(string &a,string &b){
  int n = a.size();
  int m = b.size();
  vector <vector<int> > dp(n+1,vector<int>(m+1));

  for(int i=0;i<=n;i++)
    for(int j=0;j<=m;j++){
      if(i<n&&j<m&&a[i] == b[j]) Max(dp[i+1][j+1],dp[i][j]+1);
      Max(dp[i+1][j],dp[i][j]);
      Max(dp[i][j+1],dp[i][j]);
    }
  return dp[n][m];
}

int main(){
  int q;
  cin>>q;
  while(q--){
    string a,b;
    cin>>a>>b;
    cout<<LCS(a,b)<<endl;
  }
  return 0;
}

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

using namespace std;

int lcs(const string& a, const string& b) {
    int dp[1000+1][1000+1];
    int al = a.size();
    int bl = b.size();
    memset(dp, 0, sizeof(dp[0][0])*al*bl);
    for (int i = 1; i <= al; ++i) {
        for (int j = 1; j <= bl; ++j) {
            if(a[i - 1] == b[j - 1]) {
//                cout << "=" << endl;
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
            // cout << a[i] << ":" << b[j] << endl;
            // cout << i << "," << j << "," << dp[i][j] << endl;
            // cout << endl;
        }
    }
    return dp[al][bl];
}

int main() {
    int n;
    scanf("%d", &n);

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

C++: #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(v) v.begin(), v.end()
using namespace std;

string a,b;
int dp[1010][1010];
bool u[1010][1010];

int fn(int i,int j){
    if( i < 0 || j < 0 ) return 0;
    if( u[i][j] ) return dp[i][j];
    u[i][j] = true;
    int ans = max( fn(i-1,j) , fn(i,j-1) );
    if( a[i]==b[j] ) ans = max( ans , 1+fn(i-1,j-1) );
    return dp[i][j] = ans;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    for(int i=1; i<=t; i++){
        cin >> a >> b;
        for(int j=0; j<a.size(); j++) for(int k=0; k<b.size(); k++) u[j][k] = false;
        cout << fn( a.size()-1 , b.size()-1 ) << "\n";
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define REP0(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPB0(i,n) for(int i=n-1;i>=0;i--)
#define REPB1(i,n) for(int i=n;i>0;i--)
#define llong long long
#define INF 2000000000
#define NIL -1
#define N 1000
using namespace std;
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;
  C[0][0]=0;
  REP1(i,m) C[i][0]=0;
  REP1(i,n) C[0][i]=0;
  REP1(i,m){
    REP1(j,n){
      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;
  scanf("%d",&n);
  REP0(i,n){
    cin>>s1>>s2;
    printf("%d\n",LCS(s1,s2));
  }
  return 0;
}

C++: #include <bits/stdc++.h>
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;
    for(int i=0;i<n+1;i++) c[0][i]=0;
    for(int i=0;i<m+1;i++) c[i][0]=0;
    for(int i=1;i<m+1;i++){
        for(int j=1;j<n+1;j++){
            if(X[i-1]==Y[j-1]) 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(void){
    int q;
    string X,Y;
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> X >> Y;
        cout << lcs(X,Y) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<string>
using namespace std;
 
int LCS(string a, string b){
    int m=a.size();
    int n=b.size();
    int T[m+1][n+1];
    int i,j;
    for (i=0;i<m+1;i++) T[i][0]=i;
    for (j=0;j<n+1;j++) T[0][j]=j;
    for (i=1;i<m+1;i++){
        for (j=1;j<n+1;j++){
            int min=T[i-1][j]+1;
            if (min>T[i][j-1]+1){
                min=T[i][j-1]+1;
            }
            if ((a[i-1]==b[j-1])&&(min>T[i-1][j-1]+1)){
                min=T[i-1][j-1]+1;
            }
            T[i][j]=min;
        }
    }
    return m+n-T[m][n];
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m,i;
    cin >> m;
     
    string a,b;
    for (i=0;i<m;i++){
        cin >> a >> b;
        cout << LCS(a,b) << endl;
    }
    return 0;
}