Go: package main

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

func max(x, y int) int {
	if x < y {
		return y
	} else {
		return x
	}
}

/*********************************************************************
長さmの列X_(m)と長さnの列Y_(n)の最長共通部分列LCSは、
	(1)X[m] == Y[n]ならば、X_(m-1)とY_(n-1)のLCSにX[m]を繋げたもの
	(2)X[m] != Y[n]ならば、X_(m-1)とY_(n)のLCSもしくはX_(m)とY_(n-1)のLCS
	のいずれかのうち長い方

c[m+1][n+1]：c[i][j]にX_(i)とY_(j)のLCSの長さを格納する二次元配列
*********************************************************************/

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		s1 := scanner.Text()
		scanner.Scan()
		s2 := scanner.Text()
		s1, s2 = s1 + " ", s2 + " "
		c := make([][]int, len(s1))
		for i := range c {
			c[i] = make([]int, len(s2))
		}

		LCS := 0

		for i := 1; i < len(s1); i++ {
			for j := 1; j < len(s2); j++ {
				if s1[i] == s2[j] {
					c[i][j] = c[i-1][j-1] + 1
				} else {
					c[i][j] = max(c[i-1][j], c[i][j-1])
				}
				LCS = max(LCS, c[i][j])
			}
		}
		fmt.Println(LCS)
	}
}

C++: /**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

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

  int64_t Q;
  cin >> Q;
  for (int64_t q = 0; q < Q; ++q) {
    string s, t;
    cin >> s >> t;
    const int64_t SL = static_cast<int64_t>(s.length());
    const int64_t TL = static_cast<int64_t>(t.length());
    vector<vector<int64_t>> dp(SL + 1, vector<int64_t>(TL + 1, 0));
    for (int64_t i = 1; i <= SL; ++i) {
      for (int64_t j = 1; j <= TL; ++j) {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        if (s[i - 1] == t[j - 1]) {
          dp[i][j] = max(dp[i][j], 1 + dp[i - 1][j - 1]);
        }
      }
    }
    cout << dp[SL][TL] << endl;
  }
  return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#include<set>
#include<map>

using namespace std;

const int MAX_SIZE = 1000;

int LCS(string& X, string& Y,int dp[MAX_SIZE][MAX_SIZE], int X_pos, int Y_pos) {
	if (Y_pos == Y.length() || X_pos == X.length()) {
		return 0;
	}
	int res;

	if (dp[X_pos][Y_pos + 1] == -1) {
		dp[X_pos][Y_pos + 1] = LCS(X, Y, dp, X_pos, Y_pos + 1);
	}
	res = dp[X_pos][Y_pos + 1];

	for (int i = X_pos; i < X.length(); i++) {
		if (X[i] == Y[Y_pos]) {
			if (dp[i + 1][Y_pos + 1] == -1) {
				dp[i + 1][Y_pos + 1] = LCS(X, Y, dp, i + 1, Y_pos + 1);
			}
			res = max(dp[i + 1][Y_pos + 1] + 1, res);
		}
	}
	return res;
}

int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		string X, Y;
		int dp[MAX_SIZE][MAX_SIZE];

		cin >> X;
		cin >> Y;

		for (int i = 0; i < X.length() + 1; i++) {
			for (int j = 0; j < Y.length() + 1; j++) {
				dp[i][j] = -1;
			}
		}

		cout << LCS(X, Y, dp, 0, 0) << endl;
	}
	return 0;
}

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

using namespace std;

int main(){
  string X,Y;
  int n;
  cin>>n;
  int ans[n];
  int l=0;
  for(int m=0;m<n;m++){
  cin>>X>>Y;

  int N[X.size()+1][Y.size()+1];

  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      N[i][j]=1;
}
  }

  /*
  for(int i=0;i<X.size();i++){
    cout<<X[i]<<endl;
  }

for(int i=0;i<Y.size();i++){
    cout<<Y[i];
  }
  */

  
for(int i=0;i<=X.size();i++){
  N[i][0]=0;
  }

for(int i=0;i<=Y.size();i++){
  N[0][i]=0;
  }

 int point;

  for(int i=1;i<=X.size();i++){
    for(int j=1;j<=Y.size();j++){
      if(X[i-1]==Y[j-1]){
	point=1;
	
      }else{
	point=0;
      }
       N[i][j]=max({N[i-1][j],N[i][j-1],N[i-1][j-1]+point});
       point=0;
}
   
  }
  ans[l]=N[X.size()][Y.size()];
  l++;
  // cout<<N[X.size()][Y.size()]<<endl;
  /*
  //answer
  for(int i=0;i<=X.size();i++){
    for(int j=0;j<=Y.size();j++){

      cout<<N[i][j]<<" ";
      if(j==Y.size()){
	cout<<endl;
      }
}
  }
  */

  }
  cout<<ans[0]<<endl;
 cout<<ans[1]<<endl;
 cout<<ans[2]<<endl;
}

C++: #include <iostream>
#include <string>
using namespace std;

int preX[1020][30];
int dp[1020];

int main() {
	int q;
	cin >> q;
	for(int t = 0; t < q; t++) {
		string X, Y;
		for(int i = 0; i < 1020; i++) {
			for(int j = 0; j < 30; j++) {
				preX[i][j] = 0;
			}
			dp[i] = 0;
		}
		cin >> X >> Y;
		for(int i = 0; i < X.size(); i++) {
			for(int j = 0; j < 30; j++) {
				preX[i+1][j] = preX[i][j];
			}
			if(i > 0)
				preX[i+1][X[i-1]-'a'] = i;
		}
		for(int i = 0; i < Y.size(); i++) {
			for(int j = (int)X.size()-1; j >= 0; j--) {
				if(Y[i] == X[j])
					for(int k = 0; k < 30; k++)
						dp[j+1] = max(dp[j+1], dp[preX[j+1][k]] + 1);
			}
		}
		int res = 0;
		for(int i = 0; i < 1020; i++)
			res = max(res, dp[i]);
		cout << res << endl;
	}
}

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

using namespace std;

#define ll long long
typedef complex<double> P;

const int MAX_SIZE = 1001;
int q;
int dp[MAX_SIZE][MAX_SIZE] = {};

int main() {
  cin >> q;
  for (int i = 0; i < q; i++) {

    for (int j = 0; j < MAX_SIZE; j++) {
      dp[MAX_SIZE-1][0] = 0;
      dp[0][MAX_SIZE-1] = 0;
    }

    string a, b; cin >> a >> b;

    for (int j = 0; j < a.size(); j++) {
      for (int k = 0; k < b.size(); k++) {
        if(a[j] == b[k]) {
          dp[j+1][k+1] = dp[j][k] + 1;
        }
        else {
          dp[j+1][k+1] = max(dp[j][k+1], dp[j+1][k]);
        }
      }
    }

    cout << dp[a.size()][b.size()] << endl;
  }
}

C++: #include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<sstream>
#include<iomanip>
#include<limits>
#include<deque>
#include<map>
#include<list>
#include<set>
#include <unordered_set>
#include<vector>
#include<cmath>
#include<cstdio>
#include<memory>
#include<bitset>
#include<stack>
#include<functional>
#include<queue>
#include<regex>
#include<time.h>

using namespace std;

#define YES cout<<"YES"<<endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl

#define Tof(x) (x) ? Yes : No
#define TOF(x) (x) ? YES : NO
#define tof(x) (x) ? yes : no

using ll = long long;

constexpr ll MOD = 1000000007;
constexpr ll INF = 1050000000;



/*for文でx,yを上下左右確認するやつだよ。正直使いづらいよ。

int dx[] = { 1,-1, 1,-1,0,1,-1, 0 };
int dy[] = { 1,-1,-1, 1,1,0, 0,-1 };


for (int i = 0; i < 4; i++) {
	int ny = y + dy[i];
	int nx = x + dx[i];
}

*/

/*繰り返し二乗法だよ。modもとってくれるよ。n^kをmodで割った余りでやってくれるよ。

ll POW_MOD(ll n, ll k, ll mod) {
	ll r = 1;

	for (; k > 0; k >>= 1) {
		if (k & 1) {
			r = (r * n) % mod;
		}
		n = (n * n) % mod;
	}
	return r;
}

*/

/*2重for文書くのめんどくなった時用だよ。

for (int i = 0; i < h; ++i) {
	for (int j = 0; j < w; ++j) {

	}
}

*/

struct node {
	int value;
	node* next;
	node(int value, node* next) : value(value), next(next) { }
};

#define index_of(as, x) distance(as.begin(), lower_bound(as.begin(), as.end(), x))

vector<int> lcs_hs(const vector<int>& a, const vector<int>& b) {
	const int n = a.size(), m = b.size();
	map< int, vector<int> > M;
	for (int j = m - 1; j >= 0; --j)
		M[b[j]].push_back(j);
	vector<int> xs(n + 1, INF); xs[0] = -INF;
	vector< node* > link(n + 1);
	for (int i = 0; i < n; ++i) {
		if (M.count(a[i])) {
			vector<int> ys = M[a[i]];
			for (int j = 0; j < ys.size(); ++j) {
				int k = index_of(xs, ys[j]);
				xs[k] = ys[j];
				link[k] = new node(b[ys[j]], link[k - 1]);
			}
		}
	}
	int l = index_of(xs, INF - 1) - 1;
	vector<int> c;
	for (node* p = link[l]; p; p = p->next)
		c.push_back(p->value);
	reverse(c.begin(), c.end());
	return c;
}


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

	for (int i = 0; i < n; i++) {
		vector<int> a, b;
		string s; cin >> s;
		for (int j = 0; j<s.size(); j++) {
			char x = s[j];
			a.push_back((int)x);
		}
		string t; cin >> t;
		for (int j = 0; j<t.size(); j++) {
			char y = t[j];
			b.push_back((int)y);
		}
		vector<int> c = lcs_hs(a, b);
		cout << c.size() << endl;
	}


	return 0;
}