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<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<list>
#include<map>
#include<algorithm>

using namespace std;

int solve(string x, string y)
{
    vector<vector<int> > c(x.size(), vector<int>(y.size(), 0));

    if (x[0] == y[0]) {
        c[0][0] = 1;
    }

    for (int i = 1; i < x.size(); i++) {
        if (c[i - 1][0] == 1 || x[i] == y[0]) {
            c[i][0] = 1;
        } else {
            c[i][0] = 0;
        }
    }
    for (int i = 1; i < y.size(); i++) {
        if (c[0][i - 1] == 1 || x[0] == y[i]) {
            c[0][i] = 1;
        } else {
            c[0][i] = 0;
        }
    }

    for (int i = 1; i < x.size(); i++) {
        for (int j = 1; j < y.size(); 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[x.size() - 1][y.size() - 1];
}

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

    vector<string> x(q), y(q);
    for (int i = 0; i < q; i++) {
        cin >> x[i] >> y[i];
    }
    for (int i = 0; i < q; i++) {
        cout << solve(x[i], y[i]) << endl;
    }

    return 0;
}

C++: #include<iostream>
#include<string>
using namespace std;
int L[1001][1001];
int main(){
    int N;
    string X,Y;
    cin >>N;
    for(int k=0;k<N;++k){
        cin>>X>>Y;
        for(int i=1;i<X.length()+1;++i){
            for(int j=1;j<Y.length()+1;++j){
                if(X[i]==Y[j]){
                    L[i][j]=L[i-1][j-1]+1;
                }
                else
                    L[i][j]=max(L[i-1][j],L[i][j-1]);
                
            }
        }
        cout<<L[X.length()][Y.length()]<<endl;
    }
    return 0;
}

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

int main(){
  int n;
  char x[1000],y[1000];
  int dp[1001][1001];
  //load n;
  cin >>n;
  for(int i=0;i<n;i++){
    //load string
    scanf("%s",x);
    scanf("%s",y);
    //dp
    for(int j=0;j<strlen(x);j++){
      for(int k=0;k<strlen(y);k++){
	if(x[j]==y[k]){
	  dp[j+1][k+1]=dp[j][k]+1;
	}

	else{
	  if(dp[j][k+1]>dp[j+1][k]){
	    dp[j+1][k+1]=dp[j][k+1];
	  }

	  else{
	    dp[j+1][k+1]=dp[j+1][k];
	  }


	}
      }
    }
    cout <<dp[strlen(x)][strlen(y)]<<endl;
  }



  return 0;
}

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

#define N 1001
int A[N][N];

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

    string target1;
    string target2;
    for (int i = 0; i < q; i++) {
        cin >> target1;
        cin >> target2;
        int l1 = target1.length();
        int l2 = target2.length();
        for (int m = 0; m <= l1; m++) {
            A[m][0] = 0;
        }
        for (int n = 0; n <= l2; n++) {
            A[0][n] = 0;
        }

        target1 = ' ' +  target1;
        target2 = ' ' +  target2;

        for (int m = 1; m <= l1; m++) {
            for (int n = 1; n <= l2; n++) {
                if (target1[m] == target2[n]) {
                    A[m][n] = A[m-1][n-1] + 1;
                } else {
                    A[m][n] = max(A[m-1][n], A[m][n-1]);
                }
            }
        }

        cout << A[l1][l2] << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>

#define iter(n) for(int iter_var = 0; iter_var < n; iter_var += 1)
#define rep(i, s, n) for(int i = s; i < n; i += 1)
#define lli long long int
#define INF 1e9
#define MOD 1000000007

using namespace std;

int lcs(string s, string t){

  int s_size = s.size();
  int t_size = t.size();
  vector<vector<int>> dp(s_size + 1, vector<int>(t_size + 1, 0));

  rep(i, 1, s_size + 1) rep(j, 1, t_size + 1){
    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]);
    }
  }

  return dp[s_size][t_size];
}

int main(){

  int n; cin >> n;
  iter(n){
    string s, t; cin >> s >> t;
    cout << lcs(s, t) << endl;
  }

  return 0;
}