Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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

using namespace std;
static const int N = 100;

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;
	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);
		cout << endl;
	}
	return 0;
}

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

using namespace std;
int main(){
  int dataset, i, j, k, alen, blen;
  string a, b;
  cin >> dataset;
  for (i = 0; i < dataset; ++i){
    cin >> a >> b;
    alen = a.length();
    blen = b.length();
    int lcs[alen+1][blen+1];
    for (j = 0; j <= alen; ++j){
      for (k = 0; k <= blen; ++k){
        lcs[j][k] = 0;
      }
    }
    for (j = 1; j <= alen; ++j){
      for (k = 1; k <= blen; ++k){
        if(a.at(j-1) == b.at(k-1)){
          lcs[j][k] = lcs[j-1][k-1] + 1;
        }else{
          lcs[j][k] = max(lcs[j-1][k], lcs[j][k-1]);
        }
      }
    }
    cout << lcs[alen][blen] << endl;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int q;
  cin>>q;
  string a,b;
  int x[1001][1001];
  x[0][0]=0;
  for(int i=1;i<=1000;i++){
    x[0][i]=0;
    x[i][0]=0;
  }
  for(int i=0;i<q;i++){
    cin>>a>>b;
    for(int j=1;j<=b.size();j++){
      for(int l=1;l<=a.size();l++){
	if(a[l-1]==b[j-1]){
	  if(x[j-1][l-1]+1>x[j][l-1]){
	    x[j][l]=x[j-1][l-1]+1;
	  }else{
	    x[j][l]=x[j][l-1];
	  }
	}else{
	  if(x[j-1][l]>x[j][l-1]){
	    x[j][l]=x[j-1][l];
	  }else{
	    x[j][l]=x[j][l-1];
	  }
	}
      }
    }
    cout<<x[b.size()][a.size()]<<endl;
  }
  return(0);
}

C++: #include<cstdio>
#include<cstring>
#define N 1000

int LCS[N+1][N+1];

int LCSLength(char X[], int m, char Y[], int n){
	for(int i=0;i<=m;i++)
		LCS[i][n] = 0;
	for(int j=0;j<=n;j++)
		LCS[m][j] = 0;
	for(int i=m-1;i>=0;i--){
		for(int j=n-1;j>=0;j--){
			LCS[i][j] = LCS[i+1][j+1];
			if(X[i] == Y[j])
				LCS[i][j]++;
			if(LCS[i][j+1] > LCS[i][j])
				LCS[i][j] = LCS[i][j+1];
			if(LCS[i+1][j] > LCS[i][j])
				LCS[i][j] = LCS[i+1][j];
		}
	}
	return LCS[0][0];
}

int main(){
	char X[N+1],Y[N+1];
	int num;
	scanf("%d",&num);
	for(int i=1;i<=num;i++){
		scanf("%s %s",X,Y);
		int m = strlen(X);
		int n = strlen(Y);
		printf("%d\n",LCSLength(X,m,Y,n));
	}
	return 0;
}

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

#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
#define OP(m) cout << m << endl
typedef long long ll;
typedef unsigned long long ull;

int main()
{
    int n;
    cin >> n;
    rep(k, n)
    {
        string s, t;
        cin >> s >> t;
        int s_s = s.size(), t_s = t.size();
        int dp[s_s + 1][t_s + 1];
        rep(i, s_s + 1) dp[i][0] = 0;
        rep(j, t_s + 1) dp[0][j] = 0;

        rep(i, s_s)
        {
            rep(j, t_s)
            {
                if (s[i] == t[j])
                    dp[i + 1][j + 1] = dp[i][j] + 1;
                else
                    dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]);
            }
        }
        OP(dp[s_s][t_s]);
    }
    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define loop(i, a, n) for(int i = (a); i < (n); i++)
using namespace std;
using ll = int64_t;

int main(){
    int q; cin >> q;
    rep(qi, q){
        string x, y; cin >> x >> y;
        int xsize = x.size(), ysize = y.size();
        int dp[xsize+1][ysize+1];
        memset(dp, 0, sizeof(dp));
        loop(i, 1, xsize+1)loop(j, 1, ysize+1){
            if(x[i-1] == y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
        cout << dp[xsize][ysize] << endl;
    }

    return 0;
}

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

int dp[1002][1002];

int main()
{
	for (int i = 0; i < 1002; i++)
	{
		dp[0][i] = 0;
		dp[i][0] = 0;
	}
	int N;
	cin >> N;
	for (int i = 0; i < N; i++)
	{
		string str1, str2;
		cin >> str1 >> str2;
		for (int i = 1; i <= str1.size(); i++)
		{
			for (int j = 1; j <= str2.size(); j++)
			{
				if (str1[i - 1] == str2[j - 1])dp[i][j]=dp[i - 1][j - 1] + 1;
				else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		cout << dp[str1.size()][str2.size()] << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<string.h>
using namespace std;

int dp[1001][1001];
string str1, str2;
int n;

int solve(string str1, string str2) {
  memset(dp, 0, sizeof(dp));
  for(int r = 0; r < str2.size(); r++) {
    for(int c = 0; c < str1.size(); c++) {
      if(str2[r] == str1[c]) {
        dp[r + 1][c + 1] = dp[r][c] + 1;
      }
      else {
        dp[r + 1][c + 1] = max(dp[r + 1][c], dp[r][c + 1]);
      }
    }
  }
  return dp[str2.size()][str1.size()];
}

int main() {
  cin >> n;
  for(int i = 0; i < n; i++) {
    cin >> str1 >> str2;
    cout << solve(str1, str2) << endl;
  }
}

C++: #include <cmath>
#include <cstdlib>
#include <iostream>
#include <bitset>
#include <deque>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <iterator>
#include <string>
#include <chrono>
#include <random>
#include <tuple>
#include <utility>
#include <fstream>

std::string x, y;
long cnt[1005][1005];//[i][j]: x[i], y[j]までの和
long q;

int main(){
	scanf("%ld", &q);
	for(int i = 0; i < q; i++){
		std::cin >> x >> y;
		for(int i = 0; i <= x.length(); i++){
			for(int j = 0; j <= y.length(); j++){
				if(i == 0 || j == 0){
					cnt[i][j] = 0;
				}else if(x[i-1] == y[j-1]){
					cnt[i][j] = cnt[i-1][j-1] + 1;
				}else{
					cnt[i][j] = std::max(cnt[i-1][j], cnt[i][j-1]);
				}
			}
		}
		printf("%ld\n", cnt[x.length()][y.length()]);
	}
	return 0;
}