Go: package main

/*
最長共通部分列
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

C++: #include<iostream>
#include<algorithm>
#include<string>
using namespace std;
static const int Max = 1000;
int getlong(string a, string b){
int n,m,i,j,as=0;
	int cs[Max + 1][Max + 1];
	n=a.size();
	m=b.size();

	a=' '+a;
	b=' '+b;
	cs[0][0]=0;
	for(i=1;i<=n;i++)
	cs[i][0]=0;
	for(i=1;i<=m;i++)
	cs[0][i]=0;
	for(i=1;i<=n;i++){
		for(j=1;j<=m;j++){
			if(a[i]==b[j])
			cs[i][j]=cs[i-1][j-1]+1;
			else
			cs[i][j]=max(cs[i][j-1],cs[i-1][j]);
		as=max(as,cs[i][j]);
		}
		
	}
	return as;
}
main(){
	int q,i;
	string a,b;
	cin>>q;
	for(i=0;i<q;i++){
		cin>>a>>b;
		printf("%d\n",getlong(a,b));
	}
	return 0;
}

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

using namespace std;

int main() {
	int n, l1, l2;
	string s1, s2;

	cin >> n;
	while (n--) {
		cin >> s1 >> s2;
		l1 = s1.size();
		l2 = s2.size();

		int dp[l1 + 1][l2 + 1];
		for (int i = 0; i <= l1; i++) {
			for (int j = 0; j <= l2; j++) {
				dp[i][j] = 0;
			}
		}
		for (int i = 1; i <= l1; i++) {
			for (int j = 1; j <= l2; j++) {
				if (s1[i - 1] == s2[j - 1]) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				}
				else {
					dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		cout << dp[l1][l2] << endl;
	}

	return 0;
}

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

using namespace std;
int dp[1002][1002];
int main(){
    int T;
    cin>>T;
    while(T--){
        #ifdef LCS
        string s;
        cin>>s;
        int len=s.size();
        int *dp=new int[len+1];
        dp[0]=1;
        int ans=1;
        for(int i=1;i<len;i++){
            char ch=s[i];
            int cnt=0;
            for(int j=0;j<=i-1;j++){
                if(s[j]<ch) cnt=max(cnt,dp[j]);
            }
            dp[i]=cnt+1;
            ans=max(ans,dp[i]);
        }
        cout<<ans<<endl;
        #endif//LCS
        string s1,s2;
        cin>>s1>>s2;
        int len1=s1.size(),len2=s2.size();
        int len=max(len1,len2);
        for(int i=0;i<=len;i++) {
            dp[0][i]=0;
            dp[i][0]=0;
        }
        int ans=0;
        for(int i=0;i<len1;i++){
            for(int j=0;j<len2;j++){
                if(s1[i]==s2[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[len1][len2]<<endl;
    }
}

C++: #include <bits/stdc++.h>
#define LOOP(n) for (int _i = 0; _i < (n); _i++)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define RREP(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, r, n) for (int i = (r); i < (n); ++i)
#define ALL(obj) begin(obj), end(obj)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using P = pair<int, int>;
using Graph = vector<vector<int>>;

const int INF = 2100100100;
const int MOD = 1e9 + 7;

// 多次元 vector 生成
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
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;
}

int N, M, K, Q, W, H, R, C;
int dp[1100][1100];

int main() {
    // cin.tie(0);
    // ios::sync_with_stdio(false);

    cin >> Q;
    vector<int> ans;

    REP(i, Q) {
        string a, b;
        cin >> a >> b;
        memset(dp, 0, sizeof(dp));

        FOR(i, 1, (int)a.size() + 1) {
            FOR(j, 1, (int)b.size() + 1) {
                if (a[i - 1] == b[j - 1]) chmax(dp[i][j], dp[i - 1][j - 1] + 1);
                chmax(dp[i][j], dp[i - 1][j]);
                chmax(dp[i][j], dp[i][j - 1]);
            }
        }
        /*
        REP(i, (int)a.size() + 1) {
            REP(j, (int)b.size() + 1) { cout << dp[i][j] << " "; }
            cout << endl;
        }
        */
        ans.push_back(dp[(int)a.size()][(int)b.size()]);
    }

    for (auto i : ans) {
        cout << i << endl;
    }

    return 0;
}

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

using namespace std;

int lcslen[1001][1001];
int compute_lcslen(const string &x,const string &y);

int main(){
  int q;
  cin >> q;
  for(int i=0;i<q;++i){
    string x,y;
    cin >> x;
    cin >> y;
    cout << compute_lcslen(x,y) << endl;
  }

  return 0;
}

int compute_lcslen(const string &x,const string &y){
  for(int i=1;i<=x.length();++i){
    for(int j=1;j<=y.length();++j){
      if(x[i]==y[j]){
        lcslen[i][j] = lcslen[i-1][j-1]+1;
      }else{
        lcslen[i][j] = max(lcslen[i-1][j],lcslen[i][j-1]);
      }
    }
  }
  return lcslen[x.length()][y.length()];
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#define MAX 1000

using namespace std;

string s,t;
int dp[MAX + 1][MAX + 1];

int LCS(void){

  for( int i = 0 ; i < s.size() ; i++ ){
    for( int j = 0 ; j < t.size() ; 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]);
      }
    }
  }
  return dp[s.size()][t.size()];
}


int main(void){

  int n;
  cin >> n;
  while(n--){

    memset(dp, 0 , sizeof(dp) );

    cin >> s >> t;
    cout << LCS() << endl;

  }
  return 0;
}