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

int lcs(string x, string y)
{
	int m = x.size();
	int n = y.size();
	x = ' ' + x;		//x[0]に空白を挿入
	y = ' ' + y;		//y[0]に空白を挿入
	vector<vector<int>> c(m + 1, vector<int>(n + 1, 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]);
		}
	}
	return c[m][n];
}

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

	for (int i = 0; i < N; i++)
	{
		cin >> x >> y;
		cout << lcs(x, y) << endl;
	}

	return 0;
}

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

using namespace std;

int main(void){
    int num;
    cin>>num;
    
    vector<vector<string>> inputData(num,vector<string>(2));

    int p=0;
    while(p<num){
        for(int i=0;i<2;i++){
            cin>>inputData[p][i];
        }
        p+=1;
    }

    int q=0;
    while(q!=num){
        int s=inputData[q][0].length();
        int t=inputData[q][1].length();
        vector<vector<int>> Table(s,vector<int>(t));

        for(int i=0;i<s;i++){
            if(inputData[q][0][0]==inputData[q][1][i]){
                Table[0][i]=1;
            }else{
                Table[0][i]=0;
            }

            if(inputData[q][0][i]==inputData[q][1][0]){
                Table[i][0]=1;
            }else{
                Table[i][0]=0;
            }
        }

        vector<int> RMax(t);
        for(int i=0;i<t;i++){
            RMax[i]=Table[0][i];
        }

        //Table作成
        for(int i=1;i<s;i++){
            int max=0;
            for(int j=1;j<t;j++){
                if(RMax[j-1]>max){
                    max=RMax[j-1];
                }
                Table[i][j]=max;
                if(inputData[q][0][i]==inputData[q][1][j]){
                    Table[i][j]+=1;
                }
            }

            for(int k=0;k<t;k++){
                if(Table[i][k]>RMax[k]){
                    RMax[k]=Table[i][k];
                }
            }
        }

        //表示
        int ans=0;
        for(int i=0;i<s;i++){
            for(int j=0;j<t;j++){
                if(Table[i][j]>ans){
                    ans=Table[i][j];
                }
            }
        }

        cout<<ans<<endl;

        // for(int i=0;i<s;i++){
        //     for(int j=0;j<t;j++){
        //         cout<<Table[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        q+=1;
        // cout<<endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)

typedef long long ll;
typedef pair<int, int> P;

#define MAX 100005
#define INF INT_MAX

string x, y;
vector < vector<int> > memo;

int lcs(int xs, int ys)
{
    if (xs == 0 || ys == 0) return 0;
    if (memo[xs][ys]) return memo[xs][ys];

    int res;
    if (x[xs-1] == y[ys-1]) {
        res = lcs(xs-1, ys-1) + 1;
    } else {
        res = max(lcs(xs,ys-1), lcs(xs-1,ys));
    }
    memo[xs][ys] = res;
    return res;
}

int main(int, char**)
{
    int q;
    cin >> q;

    vector<int> ans;
    rep(i,q) {
        cin >> x >> y;
        int xs = x.size();
        int ys = y.size();
        memo.assign(xs+1, vector<int>(ys+1,0));
        ans.push_back(lcs(xs,ys));
    }

    rep(i,q) cout << ans[i] << endl;
    return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <map>
#include <set>
#include <queue>
#include <list>
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,a,b) for(int i=int(a);i<int(b);++i)
#define crep(i) for(char i='a';i<='z';i++)
#define psortsecond(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.second<b.second;});
#define psortfirst(A,N) sort(A,A+N,[](const pii &a, const pii &b){return a.first<b.first;});
#define All(x) (x).begin(),(x).end()
int ctoi(const char c){
  if('0' <= c && c <= '9') return (c-'0');
  return -1;
}
using namespace std;
using pii  = pair<int,int>;
long long gcd(long long a, long long b){return (b == 0 ? a : gcd(b, a%b));}
long long lcm(long long a, long long b){return a*b/gcd(a,b);}
typedef long long ll;
#define MOD 1000000007
#define EPS 10e-8

string A,B;

ll dp[1007][1007];

int LCS(string A,string B){
    REP(i,1,A.size()+1){
        REP(j,1,B.size()+1){
            if(A[i-1]==B[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[A.size()][B.size()];
}

int main(){
    ll N;cin>>N;
    rep(i,N){
        cin>>A>>B;
        cout << LCS(A,B) << endl;
    }
}

C++: #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<bitset>
#include<vector>
#include<deque>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<iomanip>
#include <functional>
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const int INF = 1 << 28;
//cout << fixed << std::setprecision(9)
//memset(a, 0, sizeof(a));
//--------------------------

int dp[1001][1001];

int lcs(string x, string y)
{	
	int n = x.size();
	int m = y.size();

	x = " " + x;
	y = " " + y;

	for (int i = 1; i <= n; i++) dp[i][0] = 0;
	for (int j = 1; j <= m; j++) dp[0][j] = 0;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= m; j++) {
			if (x[i] == y[j]){
				dp[i][j] = dp[i - 1][j - 1] + 1;
			}
			else {
				dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
	return dp[n][m];
}

int main()
{
	int q;
	string s1, s2;
	cin >> q;

	for (int i = 0; i < q; i++) {		
		cin >> s1 >> s2;
		cout << lcs(s1, s2) << 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;
	}