Go: package main

import (
	"bufio"
	"bytes"
	"fmt"
	"io"
	"os"
	"strconv"
	"strings"
)

const N = 1001

func max(x, y int) int {
	if x > y {
		return x
	}
	return y
}

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

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

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

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

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

#define MAX 1024

int LCS[MAX][MAX];

int solve(string s1,string s2){
  for(int i=1;i<=s1.length();i++){
    for(int j=1;j<=s2.length();j++){
      if(s1[i-1]==s2[j-1])
	LCS[i][j]=LCS[i-1][j-1]+1;
      else
	LCS[i][j]=max(LCS[i][j-1],LCS[i-1][j]);
    }
  }
  return LCS[s1.length()][s2.length()];
}


int main(){
  string s1, s2;
  int n;
  cin >> n;
  for(int i=0;i<n;i++){
    for(int i=0;i<MAX;i++){
      for(int j=0;j<MAX;j++){
        LCS[i][j] = 0;
      }
    }
    cin>>s1>>s2;
    cout<<solve(s1,s2)<<endl;
    /*
    for(int i=0;i<=s1.length();i++){
      for(int j=0;j<=s2.length();j++){
	cout << LCS[i][j] << "  " ;
      }
      cout << endl;
    }*/
  }

}

C++: #if 0
?????????X[0]???????????????????????\???????????¨??§???X[1],Y[1]???????????????????????????????????????
#endif

#include <iostream>
#include <string>
using namespace std;

int main(){
	int q;
	cin >> q;
	for(int a=0;a<q;++a){
		string X,Y;
		int L[1000][1000];
		cin >> X >> Y;
		for(int i=0;i<1000;++i) //???????????\????????????L????????????
			for(int j=0;j<1000;++j)
				L[i][j]=0;
	
		cin >> q;
		for(int j=1;j<X.size()+1;++j)
			for(int k=1;k<Y.size()+1;++k){
				if(X[j-1]==Y[k-1])
					L[j][k]=1+L[j-1][k-1];
				else
					L[j][k]=max(L[j-1][k],L[j][k-1]);
			}
		cout << L[X.size()][Y.size()] << endl;
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <cstdlib>
#include <list>
#define _USE_MATH_DEFINES
#include <math.h>

using namespace std;

int lcs(string X, string Y) {
	int n, m;
	int c[1001][1001] = { 0 };
	int maxlcs = 0;;

	n = X.size();
	m = Y.size();


	X = ' ' + X;
	Y = ' ' + Y;

	for (int i = 1; i <= m; i++)c[i][0] = 0;
	for (int i = 1; i <= n; i++)c[0][i] = 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][j - 1],c[i - 1][j]);

			maxlcs = max(maxlcs, c[i][j]);
		}
	}
	return maxlcs;
}

int main() {

	int n;
	string S1, S2;

	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;

constexpr int max_len = 1024;

int lcs(const string &s,const string &t){
    const int m=s.size(),n=t.size();
    vector<vector<int>> dp(m+1,vector<int>(n+1,0));

    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++){
            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]);
        }
    }

    return dp[m][n];
}

int main(){
    int n;cin>>n;
    for(int i=0;i<n;i++){
        string s,t;
        cin >> s >> t;
        cout << lcs(s,t) << endl;
    }
}

C++: #include <iostream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <map>
using namespace std;
int main() {
    int query;
    cin >> query;
    for(int q=0;q<query;q++){
        string x,y;
        cin >> x >> y;
        vector<vector<int> > dp(x.length()+1,vector<int>(y.length()+1,0));
        for(int i=0;i<x.length();i++){
            for(int j=0;j<y.length();j++){
                if(x[i] == y[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]);
                }
            }
        }
        cout << dp[x.length()][y.length()] << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(I,A,B) for(ll I = (A); I < (B); ++I)
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v
#define NUM(x,v) (POSU(x,v)-POSL(x,v))
const ll INF=1e9+7;
const ll N = 1002;
const ll M = 21;
ll q,dp[N][N];
string x,y;
vector <ll> ans;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>q;
	FOR(k,0,q){
		cin>>x>>y;
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				dp[i][j]=0;
			}
		}
		FOR(i,0,x.size()+1){
			FOR(j,0,y.size()+1){
				if(i==0||j==0)dp[i][j]=0;
				else{
					if(x[i-1]==y[j-1])dp[i][j] = max({dp[i-1][j-1]+1,dp[i][j-1],dp[i-1][j]});
					else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
				} 
			}
		}
		ans.push_back(dp[x.size()][y.size()]);
	}
	FOR(i,0,ans.size()){
		cout<<ans[i]<<endl;
	}
	return 0;
}

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

int lcs(string x, string y) {
    x = ' ' + x;
    y = ' ' + y;
    int m = x.length();
    int n = y.length();
    int memo[m][n];
    rep(i, m) memo[i][0] = 0;
    rep(i, n) memo[0][i] = 0;
    for(int i = 1; i < m; i++) {
        for(int j = 1; j < n; j++) {
            if(x[i] == y[j]) {
                memo[i][j] = memo[i - 1][j - 1] + 1;
            } else {
                memo[i][j] = max(memo[i - 1][j], memo[i][j - 1]);
            }
        }
    }

    return memo[m-1][n-1];
}

int main() {
    // 制約見て！！！デカかったらlong longにすること！
    int q;
    string x, y;
    cin >> q;
    rep(i, q) {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }
}

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

#define rep1(i, n) for (int i = 1, i##_len = (n); i <= i##_len; i++)
#define N 1050

int lcs(string X, string Y) {
    int c[N + 1][N + 1];
    int m    = X.size();
    int n    = Y.size();
    int maxV = 0;

    X = ' ' + X;
    Y = ' ' + Y;

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

int main() {
    // ios::sync_with_stdio(false);
    // cin.tie(0);
    // cout << fixed << setprecision(5);

    string s1, s2;
    int    n;
    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }

    return 0;
}