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>
using namespace std;
static const int N=1001;
int lcs(string X,string Y){
  int c[N+1][N+1];
  int m=X.length();
  int n=Y.length();

  int max1=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]);
      }
      max1=max(max1,c[i][j]);
    }
  }
  return max1;
}

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 <iostream>

using namespace std;

int main(){
	int q;
	scanf(" %d", &q);
	for(int i=0; i<q; ++i){
		string a, b;
		cin >> a >> b;
		
		int m=a.size(), n=b.size();
		
		string x=" "+a;
		string y=" "+b;
		
		int c[1001][1001]={0};
		for(int i=1; i<=m; ++i) c[i][0]=0;
		for(int i=1; i<=n; ++i) c[0][n]=0;
		
		int ans=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]);
				
				ans=max(ans,c[i][j]);		
			}
		}
		
		printf("%d\n", ans);
	}
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <climits>
#include <tuple>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef tuple< int,int,int > T;
const int MAX_N=55555;
bool is_prime[MAX_N];
int prime[MAX_N];
int main(){
  int n;
  cin>>n;
  int dp[1001][1001];
  for(int nn=0;nn<n;nn++){
    string s1,s2;
    cin>>s1>>s2;
    for(int i=0;i<1001;i++)fill(dp[i],dp[i]+1001,0);
    for(int i=0;i<s1.size();i++){
      for(int j=0;j<s2.size();j++){
        if(s1[i]==s2[j]) dp[i+1][j+1]=max(dp[i][j]+1,max(dp[i][j+1],dp[i+1][j]));
        else dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
      }
    }
    cout<<dp[s1.size()][s2.size()]<<endl;
  }
  return 0;
}

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

vector<vector<int>> c(1001,vector<int>(1001));

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

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

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }

  cout<<N[X.size()][Y.size()];
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }

}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>

#ifdef DEBUG

#define printd printf

#else

#define printd(...)

#endif


int lcs(std::string &x, std::string &y) {
    static int dp[1002][1002];

    for (int i = 0; i < x.length() + 2; i++) {
        for (int j = 0; j < y.length() + 2; j++) {
            dp[i][j] = 0;
        }
    }

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

    #ifdef DEBUG
    for (int i = 1; i <= x.length(); i++) {
        for (int j = 1; j <= y.length(); j++) {
            std::cout << dp[i][j] << ", ";
        }
        std::cout << std::endl;
    }
    #endif

    return dp[x.length()][y.length()];
}

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

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

    return 0;
}