Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

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

using namespace std;

int lcs(const string& a, const string& b) {
    int dp[1000+1][1000+1];
    int al = a.size();
    int bl = b.size();
    memset(dp, 0, sizeof(dp[0][0])*al*bl);
    for (int i = 1; i <= al; ++i) {
        for (int j = 1; j <= bl; ++j) {
            if(a[i - 1] == b[j - 1]) {
//                cout << "=" << endl;
                dp[i][j] = dp[i - 1][j - 1] + 1;
            } else {
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
            }
            // cout << a[i] << ":" << b[j] << endl;
            // cout << i << "," << j << "," << dp[i][j] << endl;
            // cout << endl;
        }
    }
    return dp[al][bl];
}

int main() {
    int n;
    scanf("%d", &n);

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

C++: #include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fi first
#define se second
#define pb push_back
#define pii pair<int,int>
#define pll pair<long long,long long>
#define all(v) v.begin(), v.end()
using namespace std;

string a,b;
int dp[1010][1010];
bool u[1010][1010];

int fn(int i,int j){
    if( i < 0 || j < 0 ) return 0;
    if( u[i][j] ) return dp[i][j];
    u[i][j] = true;
    int ans = max( fn(i-1,j) , fn(i,j-1) );
    if( a[i]==b[j] ) ans = max( ans , 1+fn(i-1,j-1) );
    return dp[i][j] = ans;
}

int main()
{

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int t;
    cin >> t;
    for(int i=1; i<=t; i++){
        cin >> a >> b;
        for(int j=0; j<a.size(); j++) for(int k=0; k<b.size(); k++) u[j][k] = false;
        cout << fn( a.size()-1 , b.size()-1 ) << "\n";
    }

    return 0;
}

C++: #include <bits/stdc++.h>
#define REP0(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define REPB0(i,n) for(int i=n-1;i>=0;i--)
#define REPB1(i,n) for(int i=n;i>0;i--)
#define llong long long
#define INF 2000000000
#define NIL -1
#define N 1000
using namespace std;
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;
  REP1(i,m) C[i][0]=0;
  REP1(i,n) C[0][i]=0;
  REP1(i,m){
    REP1(j,n){
      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;
  scanf("%d",&n);
  REP0(i,n){
    cin>>s1>>s2;
    printf("%d\n",LCS(s1,s2));
  }
  return 0;
}

C++: #include <bits/stdc++.h>
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;
    for(int i=0;i<n+1;i++) c[0][i]=0;
    for(int i=0;i<m+1;i++) c[i][0]=0;
    for(int i=1;i<m+1;i++){
        for(int j=1;j<n+1;j++){
            if(X[i-1]==Y[j-1]) 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 maxl;
}
int main(void){
    int q;
    string X,Y;
    cin >> q;
    for(int i=0;i<q;i++){
        cin >> X >> Y;
        cout << lcs(X,Y) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<limits.h>
#include<string>
using namespace std;
 
int LCS(string a, string b){
    int m=a.size();
    int n=b.size();
    int T[m+1][n+1];
    int i,j;
    for (i=0;i<m+1;i++) T[i][0]=i;
    for (j=0;j<n+1;j++) T[0][j]=j;
    for (i=1;i<m+1;i++){
        for (j=1;j<n+1;j++){
            int min=T[i-1][j]+1;
            if (min>T[i][j-1]+1){
                min=T[i][j-1]+1;
            }
            if ((a[i-1]==b[j-1])&&(min>T[i-1][j-1]+1)){
                min=T[i-1][j-1]+1;
            }
            T[i][j]=min;
        }
    }
    return m+n-T[m][n];
}
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int m,i;
    cin >> m;
     
    string a,b;
    for (i=0;i<m;i++){
        cin >> a >> b;
        cout << LCS(a,b) << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
#include <cctype>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <list>
#include <stdio.h>
#include <string.h>
#include <cstdlib>
#include <math.h>
#include <bitset>
#include <iterator>
#include <iomanip>
#include <sstream>
#define INF 2000000000
using namespace std;
int dx[4]={1,0,-1,0},dy[4]={0,1,0,-1};
#define sym cout<<"---------"<<endl;
#define ll long long
#define PL pair<long long, long long>
#define P pair<int,int>
#define mk make_pair
#define en endl
#define WHITE 0
#define BLACK 2
#define GRAY 1
#define RE return 0
#define int ll
//
//vector<int> G[20];
//
//// 巡回セールスマン問題
//
//signed main(){
//  int v,e;
//  cin>>v>>e;
//
//  for(int i=0; i<e; i++){
//    int a,b;
//    cin>>a>>b;
//    G[a].push_back(b);
//  }
//}























////  最長共通部分列

int dp[1005][1005];

signed main(){
  int n;
  cin>>n;
  for(int i=0; i<n; i++){
    string s1,s2;
    cin>>s1>>s2;
    s1=" "+s1;
    s2=" "+s2;
    int maxl=0;
    for(int i=0; i<=s1.size()-1; i++) for(int j=0; j<=s2.size(); j++) dp[i][j]=0;
    for(int i=1; i<=s1.size()-1; i++){
      for(int j=1; j<=s2.size(); j++){
        if(s1[i]==s2[j]) dp[i][j]=dp[i-1][j-1]+1;
        else dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
        maxl=max(maxl,dp[i][j]);
      }
    }
    cout<<maxl<<en;
  }
}