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 <bits/stdc++.h>
using namespace std;

using ll = long long;
using Field = vector<vector<ll>>;
using Graph = vector<vector<ll>>;
using VI = vector<int>;
using VL = vector<ll>;
using VVL = vector<vector<ll>>;
using VC = vector<char>;
using PI = pair<int, int>;

#define FOR(i, s, n) for (int i = s; i < (n); i++)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) x.begin(), x.end()

const long long INF = 1LL<<60;
const int mod = 1000000007;

template<class T> inline bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; }

int q;


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

    cin >> q;

    REP(_, q) {

        string A, B;
        vector<vector<int>> dp(1010, vector<int>(1010));

        cin >> A >> B;

        for (int i = 0; i < A.size(); i++) {
            if (A[i] == B[0]) dp[i][0] = 1;
            chmax(dp[i+1][0], dp[i][0]);
        }
        for (int i = 0; i < B.size(); i++) {
            if (A[0] == B[i]) dp[0][i] = 1;
            chmax(dp[0][i + 1], dp[0][i]);
        }
        
        for (int i = 1; i < A.size(); i++) {
            for (int j = 1; j < B.size(); j++) {
                if (A[i] == B[j]) {
                    chmax(dp[i][j], dp[i - 1][j - 1] + 1);
                } else {
                    chmax(dp[i][j], dp[i][j-1]);
                    chmax(dp[i][j], dp[i-1][j]);
                }
            }
        }



        cout << dp[A.size() - 1][B.size() - 1] << 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 <cmath>
using namespace std;

int LCS(string str1, string str2)
{
  int n1, n2;
  n1 = (int)(str1.length());
  n2 = (int)(str2.length());
  int dp[n1+1][n2+1];

  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      dp[i][j] = 0;
    }
  }


  int maxi, s, t;
  for(int i = 1; i < n1 + 1; i++) {
    for(int j = 1; j < n2 + 1; j++) {
      if(str1[i-1] != str2[j-1]) {
	dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
      }
      else {
	dp[i][j] = dp[i-1][j-1] + 1; //(i-1,j)と(i,j-1)の場合はstr1[i]とstr2[j]が考慮されているので斜め移動のみ
      }
    }
  }
  /*
  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      cout << dp[i][j] << " ";
    }
    cout << "\n";
  }
  */
  maxi = 0;
  for(int i = 0; i < n1 + 1; i++) {
    for(int j = 0; j < n2 + 1; j++) {
      if(maxi < dp[i][j]) {
	maxi = dp[i][j];
      }
    }
  }
  

  return maxi;
}

int main()
{
  int q;
  cin >> q;
  string str1[q], str2[q];
  for(int i = 0; i < q; i++) {
    cin >> str1[i];
    cin >> str2[i];
  }

  for(int i = 0; i < q; i++) {
    cout << LCS(str1[i], str2[i]) << "\n";
  }

  return 0;
}

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

#define MAX 1000

int lcs(string X, string Y) {
    int c[MAX+1][MAX+1];
    int m = X.size();
    int n = Y.size();
    int i, j;
    int maxlen = 0;
    X = " " + X;
    Y = " " + Y;

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

int main (void) {
    int N, i;
    string x, y;

    cin >> N;

    for(i = 0 ; i < N ; i++) {
        cin >> x;
        cin >> y;
        printf("%d\n", lcs(x,y));
    }

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

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 <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(long long i=0;i<n;i++)
#define P pair<ll,ll>
#define F first
#define S second
#define all(a) (a.begin(),a.end())
long long INF=1000000000000000000;
long long mod=1000000007;
int dp[1010][1010];
int main(void){
    int q;cin>>q;string x,y;
    rep(k,q){
        cin>>x>>y;
        rep(i,1010)rep(j,1010)dp[i][j]=0;
        
        rep(i,x.size())rep(j,y.size()){
            if(x[i]==y[j])dp[i+1][j+1]=dp[i][j]+1;
            else dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j]);
        }
        cout<<dp[x.size()][y.size()]<<endl;
    }
}

C++: #include<iostream>
#include<string>
#include<algorithm>
int main(void){
    int cnt;
    int n;
    std::cin>>n;

    for(int k=0;k<n;k++){
        std::string str1;
        std::string str2;
        std::cin>>str1;
        std::cin>>str2;
        int l1=str1.size();
        int l2=str2.size();
        int A[l1][l2]={};

        for(int i=0;i<l1;i++){
            for(int j=0;j<l2;j++){
                if(i==0 or j==0){
                    if(str1[i]==str2[j]){
                        A[i][j]=1;
                    }else{
                        A[i][j]=0;
                    }
                }else if(str1[i]==str2[j]){
                    A[i][j]=A[i-1][j-1]+1;
                }else{
                    A[i][j]=std::max(A[i][j-1],A[i-1][j]);
                }
                //std::cout<<A[i][j]<<" ";
            }
            //std::cout<<"\n";
        }
        std::cout<<A[l1-1][l2-1]<<"\n";
    }
    return 0;
}