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(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	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] = dp[i-1][j-1]+1
				} else if S[i-1] != T[j-1] && dp[i-1][j] > dp[i][j-1] {
					dp[i][j] = dp[i-1][j]
				} else {
					dp[i][j] = dp[i][j-1]
				}
				// 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 <algorithm>
#include <string>
using namespace std;

int N;
string seq1,seq2;

int S_search(int a, int b){
	if (a<=0 || b<=0){
		return 0;
	}else if (seq1[a-1] == seq2[b-1]){
		return S_search(a-1,b-1)+1;
	}else{
		return max(S_search(a-1, b),S_search(a, b-1));
	}
}

int main() {
	cin >> N;
	for (int M=0; M<N; M++){
		cin >> seq1 >> seq2;
		cout << S_search(seq1.length(), seq2.length()) << endl;
	}
	
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

int main(){
  int q;
  cin >> q;
  string x, y;
  rep(_, q){
    cin >> x >> y;
    int n = x.size();
    int m = y.size();
    vector<vector<int>> dp(n+1, vector<int>(m+1, 0));
    rep(i, n)rep(j, m){
      if (x[i] == y[j]) dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j]+1);
      dp[i+1][j+1] = max(dp[i+1][j+1], dp[i+1][j]);
      dp[i+1][j+1] = max(dp[i+1][j+1], dp[i][j+1]);
    }
    cout << dp[n][m] << endl;
  }
  
  return 0;
}

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

int Q;
string X,Y;
int N,M;
int dp[1001][1001];

int solve(int xd,int yd){
  if( dp[xd][yd] != -1 ) return dp[xd][yd];
  if( xd==N || yd==M ) return dp[xd][yd] = 0;
  int ret;
  if( X[xd] == Y[yd] )
    ret = solve( xd+1,yd+1 ) + 1;
  else
    ret = max( solve(xd+1,yd), solve(xd,yd+1) );

  return dp[xd][yd] = ret;
}

int main(){
  cin >> Q;
  for(int ttt=0;ttt<Q;ttt++){
    cin >>X >> Y;
    N = X.size(); M = Y.size();
    memset(dp,-1,sizeof(dp));
    cout << solve(0,0) << endl;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>
#include <queue>

using namespace std;

const char *X, *Y;
int X_length, Y_length;

int table_lcs[1001][1001];

int lcs(int x_begin, int y_begin) {
	int i, j, to;
	int max = 0;
	int v_lcs;
	bool alreadySeenTable[256];
	if (table_lcs[x_begin][y_begin] != -1) {
		return table_lcs[x_begin][y_begin];
	}
	
	for (i = 0; i < 256; i++) {
		alreadySeenTable[i] = false;
	}
	i = x_begin;
	to = Y_length-1;
	while(i <= X_length-1) {
		j = y_begin;
		if (alreadySeenTable[X[i]] == true) {
			i++;
			continue;
		}
		bool found = false;
		while (j <= to) {
			if (X[i] == Y[j]) {
				v_lcs = lcs(i + 1, j + 1) + 1;
				if (v_lcs > max) {
					max = v_lcs;
				}
				found = true;
				break;
			}
			else {
				j++;
			}
		}
		if (found == false) {
			alreadySeenTable[X[i]] = true;
			i++;
		}
		else {
			alreadySeenTable[X[i]] = true;
			i++;
			to = j - 1;
		}
	}
	return table_lcs[x_begin][y_begin] = max;
}

int main() {

	int N;
	cin >> N;
	string stringX, stringY;

	for (int i = 1; i <= N; i++) {
		for (int i = 0; i < 1001; i++) {
			for (int j = 0; j < 1001; j++) {
				table_lcs[i][j] = -1;
			}
		}
		cin >> stringX >> stringY;
		X = stringX.c_str();
		X_length = stringX.size();
		Y = stringY.c_str();
		Y_length = stringY.size();
		printf("%d\n", lcs(0, 0));
	}
	return 0;
}

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

#define int long long
#define vvi vector<vector<int>>
#define vec vector
#define pq priority_queue
#define all(v) (v).begin(), (v).end()
#define uniqueV(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define rep(i, n) for (int (i) = (0); (i) < (n); ++(i))
#define repp(i, m, n) for (int (i) = (m); (i) < (n); ++(i))
#define dbg(x) cerr << #x << ": " << x << endl;
#define dbg2(x, y) cerr<<"("<<#x<<", "<<#y<<") = "<<"("<<x<<", "<<y<<")"<<endl;
#define dbg3(x, y, z) cerr<<"("<<#x<<", "<<#y<<", "<<#z<<") = "<<"("<<x<<", "<<y<<", "<<z<<")"<<endl;
#define dbgB(value, size) cerr<<#value<<": "<<bitset<size>(value) << endl;
#define line() cerr << "---------------" << endl;

const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, -1, 1};
const double PI = 3.14159265358979323846;

template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; }

template<typename T>
void printA(vector<T> &printArray, char between = ' ') {
   int paSize = printArray.size();
   for (int i = 0; i < paSize; i++) { cerr << printArray[i] << between;}
   if (between != '\n') {cerr << endl;}
}

// ------------------------------------------------------------------------------------------

int dp[11111][11111];

void print(string s, string t) {
   int n = s.size(), m = t.size();
   rep (i, n+1) {
      rep (j, m+1) {
         printf("%2lld ", dp[i][j]);
      }
      puts("");
   }
}

int dist(string s, string t) {
   fill(dp[0], dp[1010], 1 << 28);
   dp[0][0] = 0;
   dp[0][0] = 0;
   int N = s.size(), M = t.size();
   rep (i, N+1) dp[i][0] = i;
   rep (i, M+1) dp[0][i] = i;

   rep (i, N) {
      rep (j, M) {
         dp[i+1][j+1] = min({dp[i][j+1]+2, dp[i+1][j]+2, dp[i][j] + (s[i] != t[j])});
      }
   }

   cout << dp[N][M] << endl;
}

int lcs(string s, string t) {
   for (int i = 0; i < s.size(); i++) {
      for (int j = 0; j < t.size(); j++) {
         dp[i+1][j+1] = (s[i] == t[j] ? dp[i][j]+1 : max(dp[i][j+1], dp[i+1][j]));
      }
   }
   return dp[s.size()][t.size()];
}

signed main() {
   int n;
   cin >> n;

   while (n--) {
      string s, t;
      cin >> s >> t;

      cout << lcs(s, t) << endl;
   }

   return 0;
}