Go: package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

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 {
	lenX, lenY := len(strX), len(strY)
	memo := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		memo[i] = make([]int, lenY+1)
	}

	lcs := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if strX[i-1] == strY[j-1] {
				memo[i][j] = memo[i-1][j-1] + 1
			} else {
				if memo[i-1][j] > memo[i][j-1] {
					memo[i][j] = memo[i-1][j]
				} else {
					memo[i][j] = memo[i][j-1]
				}
			}
			if memo[i][j] > lcs {
				lcs = memo[i][j]
			}
		}
	}
	return lcs
}

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 <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;
}

C++: #define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cfloat>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <functional>
#include <stdexcept>

typedef long long ll;
typedef unsigned int ui;
typedef unsigned short us;
typedef unsigned long long ull;

template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<std::vector<T>>& v) {
  if (!v.empty()) {
    for (std::vector<T> v2 : v) {
      std::copy(v2.begin(), v2.end(), std::ostream_iterator<T>(out, ", "));
      out << "\b\b \n";
    }
  }
  return out;
}
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
  if ( !v.empty() ) {
    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b ";
  }
  return out;
}

using namespace std;

namespace my {
  class LCS
  {
  public:
    LCS() = default;
    ~LCS() = default;

  private:

  };
}

int main(int argc, const char * argv[]) {
  int q;
  cin >> q;
  vector<string> x;
  vector<string> y;
  for (size_t i = 0; i < q; i++)
  {
    string temp;
    cin >> temp;
    x.push_back(temp);
    cin >> temp;
    y.push_back(temp);
  }
  
  
  
  for (size_t i = 0; i < q; i++)
  {
    size_t x_length = x[i].length();
    size_t y_length = y[i].length();
    size_t max_length = min(x_length, y_length);
    vector<int> length_y_nanmoji(1, y_length);
    map<char, vector<int>> y_nanmoji;
    for (size_t j = 0; j < x_length; j++)
    {
      char x_char = x[i][j];
      if (y_nanmoji.find(x_char) == y_nanmoji.end()) {
        size_t count = 0;
        bool is_looped = false;
        do
        {
          count = y[i].find(x_char, count);
          if (count >= y_length) {
            if (!is_looped) {
              y_nanmoji[x_char].push_back(-1);
            }
            break;
          }
          else {
            y_nanmoji[x_char].push_back((int)count);
            count++;
          }
        } while (is_looped = true, true);
      }

      auto y_nanmoji_now = y_nanmoji[x_char];
      if (y_nanmoji_now[0] == -1) {
        continue;
      }
      for(int l = (int)y_nanmoji_now.size() - 1; l >= 0; l--)
      {
        int val = y_nanmoji_now[l];
        bool is_done = false;
        for (int k = 0; k < (int)length_y_nanmoji.size(); k++)
        {
          if (length_y_nanmoji[k] >= val) {
            length_y_nanmoji[k] = val;
            is_done = true;
            break;
          }
        }
        if (!is_done) {
          length_y_nanmoji.push_back(val);
        }
      }

    }
    int kotae = length_y_nanmoji[0] >= y_length ? 0 : length_y_nanmoji.size();
    cout << kotae << endl;
  }

  return 0;
}

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 <iostream>
#include <string>
#include <cstring> //memset(dp,0,sizeof(dp))
#define ll long long
#define rep(i,n) for(int i=0;i<(n);i++)
#define repp(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
typedef pair<int,int> P;
//小数点以下　cout << fixed << setprecision(10) << num << endl;
//桁数はlog10(mid)でやれ　log(mid)/log(10)はWA
//long*long<0で判定するな　オーバーフローする
//開区間と閉区間どうにかしろ
//アルファベットが何番目かは x=s[i]-'a'
const ll MOD = 1000000007;

int LCS(string x,string y){
    int nx=x.size();
    int ny=y.size();
    int dp[nx][ny];
    //dp[i][j]:=xを上からi個、yを上からj個見たときの最長部分文字列の長さ
    memset(dp,0,sizeof(dp));
    if(x[0]==y[0]) dp[0][0]=1;
    else dp[0][0]=0;
    //dp[i+1][j]= (if x[i+1]==y[j]:dp[i][j]+1 else dp[i][j])
    rep(i,nx){
        rep(j,ny){
            if(x[i]==y[j]){
                if(i>0 && j>0) dp[i][j]=dp[i-1][j-1]+1;
                else dp[i][j]=1;
            }
            else if(i>0 && j>0) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
            else if(i>0) dp[i][j]=dp[i-1][j];
            else if(j>0) dp[i][j]=dp[i][j-1];
        }
    }
    return dp[nx-1][ny-1];
}

int main(){
    int q;
    cin >> q;
    rep(i,q){
        string x,y;
       cin >> x >> y;
        cout << LCS(x,y) << endl;
    }
}