Go: package main

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

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

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <functional>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <bitset>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
int dx[4] = {1, 0, 0, -1};
int dy[4] = {0, 1, -1, 0};

void slove(string X, string Y){
	int dp[1000][1000];//dp[X?????????][Y?????????] = LCS
	int sizex = X.size();
	int sizey = Y.size();

	//???????????????????????????????????????????????????0????????????????????\?????????
	X = ' ' + X;
	Y = ' ' + Y;

	reps(i, 1, sizex + 1) dp[i][0] = 0;//?????????????????????0??????dp???0
	reps(i, 1, sizey + 1) dp[0][i] = 0;//?????????????????????0??????dp???0

	int ans = 0;//????°??????????
	reps(i, 1, sizex + 1){
		reps(j, 1, sizey + 1){
			if(X[i] == Y[j]) dp[i][j] = dp[i - 1][j - 1] + 1;
			else if(X[i] != Y[j]) dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);

			ans = max(ans, dp[i][j]);
		}
	}
	printf("%d\n", ans);
	return;
}

int main(void){
	int n; cin >> n;
	string s1, s2;
	rep(i, n){
		cin >> s1 >> s2;
		slove(s1, s2);
	}
	return 0;
}

C++: #include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
string a;
string b;
int ans[1000+1][1000+1];
int lcs()
{
    int al=a.size();
    int bl=b.size();
    int maxn=-1;
    a=" "+a;
    b=" "+b;
    for (int i=1;i<=al;i++)
    {
        for (int j=1;j<=bl;j++)
        {
            if(a[i]==b[j])
            {
                ans[i][j]=ans[i-1][j-1]+1;
            }
            else {
                ans[i][j]=max(ans[i-1][j],ans[i][j-1]);
            }
            if(ans[i][j]>maxn)
                maxn=ans[i][j];
        }
    }
   return maxn;
}
int main()
{
    int q;
    scanf("%d",&q);
    for(int i=1;i<=q;i++)
    {
        cin>>a;
        cin>>b;
        int sum=lcs();
        cout<<sum<<endl;
    }
    return 0;
}

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

int main(){
  int q;
  cin >> q;
  string X,Y;

  for(int i=0;i<q;i++){
    cin >> X >> Y;
    int dp[1001][1001]={};
    for(int j=1;j<=Y.size();j++){
      for(int k=1;k<=X.size();k++){
	if(X[k-1]==Y[j-1]) dp[j][k]=dp[j-1][k-1]+1;
	else dp[j][k]=max(dp[j-1][k],dp[j][k-1]);
      }
    }
    cout << dp[Y.size()][X.size()] << endl;
  }
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) for(int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;
const int INF = 2000000000;
using namespace std;


//????????±?????¨??????
const int MAX_N = 1000;
int dp[MAX_N + 1][MAX_N + 1];

int solve(string s, string t, int n, int m){
    rep(i,n){
        rep(j,m){
            if(s[i] == t[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]);
            }
        }
    }
    return dp[n][m];
}

int main(){
    int n;
    string s, t;
    cin >> n;
    rep(i,n){
        cin >> s >> t;
        cout << solve(s, t, s.size(), t.size()) << endl;
    }
}

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

int dp[1000+1][1000+1];

int main() {
    int n; cin >> n;
    while(n--) {
        string s, t; cin >> s >> t;
        int N = s.length();
        int M = t.length();
        dp[0][0] = 0;
        for(int i = 0; i < N; i++) {
            for(int j = 0; j < M; 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]);
            }
        }
        cout << dp[N][M] << endl;
    }
}