Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

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;
}