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<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
map <char,vector<int>> alx,aly;//x,y
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//yn文字目以降でX[xn]と同じやつを探す
    if(memo[xn][yn]!=-1)return memo[xn][yn];
    int ans=0;
	/*
    for(int i=xn; i<x.size();i++){
        for(int j=yn; j<y.size(); j++){
            if(alx[x[i]]==aly[y[j]]){
                ans=max(ans,lcs(i+1,j+1)+1);
            }
        }
    }
	*/
	for(int i=xn; i<x.size();i++){
        for(int j=0; j<aly[x[i]].size(); j++){
			if(aly[x[i]][j] < yn) continue;
			ans=max(ans,lcs(i+1,aly[x[i]][j]+1)+1);
			break;
		}
    }
    return memo[xn][yn]=ans;
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++){
				memo[i][j]=-1;
			}
        }

		alx.clear();
		aly.clear();

		for(int i=0; i<x.size(); i++) alx[x[i]].push_back(i);
		for(int i=0; i<y.size(); i++) aly[y[i]].push_back(i);
		

        //memset(memo, -1, sizeof(memo));
         
        cout << lcs(0,0) << endl;
 
    }
    return 0;
}



/*
int memo[1010][1010];
string x,y,q;
int lcs(int xn,int yn){//Y[yn]とX[xn]を比較
    if(memo[xn][yn]!=-1)return memo[xn][yn];
	else if(xn==0 || yn==0)return 0;
	else if(x[xn]==y[yn])return memo[xn][yn] = lcs(xn-1,yn-1)+1;
	else  return memo[xn][yn] = max(lcs(xn,yn-1),lcs(xn-1,yn));
}
 
 
int main(){
    int q;
    cin >> q;
    for(int ds=0; ds<q; ds++){
 
        cin >> x >> y;
        for(int i=0; i<=x.size(); i++){
            for(int j=0; j<=y.size(); j++)memo[i][j]=-1;
        }
        //memset(memo, -1, sizeof(memo));
         
        cout << max(lcs(x.size(),y.size()),0) << endl;
 
    }
    return 0;
}*/

/*

#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
int n,memo[50]={};

int fib(int n){
	if(memo[n]!=0)return memo[n];
	return memo[n]=fib(n-1)+fib(n-2);
}


int main(){

	cin >> n;
	memo[0]=1;
	memo[1]=1;
	for(int i=2; i<=n; i++)
		memo[i]=memo[i-1]+memo[i-2];
	cout << memo[n] << endl;
	return 0;
}
*/

/*
#include<queue>
using namespace std;
int n,A[2000],q;//A:0origin
int memo[2000][2000];

bool solve(int i,int m){//i番目以降のAを使ってmを作る
	if(m==0) return true;//０はどんなときも作れる
	if(i==n) return false;//すべて使ってもつくれない
	//すでにやってるときメモを返す
	if(memo[i][m]!=0){
		if(memo[i][m]==-1)return false;
		else return true;
	}
	//solve(i,m)  A[i]を使う solve(i+1,m-A[i]) || 使わない　solve(i+1,m)
	bool X=false;
	if(m-A[i]>=0 && solve(i+1,m-A[i])) X=true;
	else if(solve(i+1,m)) X = true;

	if(X)memo[i][m]=1;
	else memo[i][m]=-1;

	 return X;

}

int main(){

	cin >> n;

	for(int i=0; i<n; i++) cin >> A[i];

	
	cin >> q;

	for(int i=0; i<q; i++){
		int m;
		cin >> m;
		for(int k=0; k<n; k++){
			for(int j=0; j<n; j++) memo[k][j]=0;
		}

		if(solve(0,m))cout << "yes" << endl;
		else cout << "no" << endl; 
	}

	return 0;
}

*/

/*
	bool dp[2000][2000];
	bool memo[2000][2000];//[順番][m-これまでの総和]
	
	bool solve(int now,int least){
		if(now == n) return false;//now>nならfalse
		else if(least == 0)return true;//least==0 >> true
		else if(memo[now][least])return dp[now][least];//already diside >> そのまま出来上がってるのを返す
		else if(solve(now+1,least))dp[now][least] = true;//A[now]をとらない >> solve(now++,least)
		else if(least-A[now] > 0 && solve(now++,least-A[now]))dp[now][least] = true;//A[now]をとる (least-A[now]>0 only)>> solve(now++,least-A[now])
		else dp[now][least] = false;
		memo[now][least] = true;
		return dp[now][least];
	}
	*/

C++: #include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(a); (i)<(b); (i)++)
#define rFor(i, a, b) for(int (i)=(a)-1; (i)>=(b); (i)--)
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
using namespace std;

string x, y;
int n, m;
int dp[1010][1010];

int solve(int i, int j){
    if(i==0 || j==0) return dp[i][j] = 0;
    if(dp[i][j] >= 0) return dp[i][j];
    if(x[i-1] == y[j-1]){
        dp[i][j] = max(solve(i-1, j), solve(i, j-1));
        return dp[i][j] = max(dp[i][j], solve(i-1, j-1)+1);
    }
    return dp[i][j] = max(solve(i-1, j), solve(i, j-1));
}

int main(){
    int q; cin >> q;
    rep(i, q){
        cin >> x >> y;
        n = (int)(x.size()); m = (int)(y.size());
        rep(i, n+1)rep(j, m+1) dp[i][j] = -1;
        cout << solve(n, m) << endl;
    }
}

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

using namespace std;                                         
                                                             
//L[i][j] X[0]..X[i]??¨X[0]..Y[j]???LCS                        
int L[1001][1001]={};                                        
int main(){
  int testnum;
  cin>>testnum;                                              
  for(int t=0;t<testnum;++t){                                
    string X,Y;                                              
    cin>>X>>Y;
    
    for(int i=1;i<=X.length();++i){                          
      for(int j=1;j<=Y.length();++j){                        
        if(X[i-1]==Y[j-1])
          L[i][j] = 1 + L[i-1][j-1];                         
        else
          L[i][j] = max(L[i][j-1],L[i-1][j]);
      }
    }

    cout << L[X.length()][Y.length()] << endl;
  }
 
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int dp[1005][1005] = { 0 };
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string x, y;
		cin >> x >> y;
		for (int j = 0; j < 1005; j++)
			for (int k = 0; k < 1005; k++) dp[j][k] = 0;
		for (int j = 1; j <= x.size(); j++) {
			for (int k = 1; k <= y.size(); k++) {
				dp[j][k] = max(dp[j][k - 1], dp[j - 1][k]);
				if (x[j - 1] == y[k - 1]) {
					dp[j][k] = max(dp[j][k], dp[j - 1][k - 1] + 1);
				}
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}
	return 0;
}