Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func maxInt(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func minInt(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

C++: #include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>
#include <utility>
using namespace std;
//_____
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0; i<(n); ++i)
#define reps(i,f,n) for(int i=(f); i<=(n); ++i)
#define err(...) fprintf(stderr, __VA_ARGS__)
#define show(x) cerr << #x << ':' << x << endl
#define outl(x) cout << (x) << '\n'
#define FAST() cin.tie(0), ios::sync_with_stdio(false)
typedef long long ll;
typedef pair<int,int> pii;
template<class A, class B>inline bool chmax(A &a, B b){return b>a ? a=b,1 : 0;}
template<class A, class B>inline bool chmin(A &a, B b){return b<a ? a=b,1 : 0;}
inline bool inside(int y, int x, int H, int W){return(y>=0 && x>=0 && y<H && x<W);}
const int INF = 1 << 29;
const int dx[] = {1, 0, -1, 0,  1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1,  -1, 1, 1, -1};
#define LEN 1000011
#define NIL -1
//_______

int dp[1005][1005];

int slv(string &a, string &b)
{
   const int alen = a.size();
   const int blen = b.size();
   a = ' ' + a;
   b = ' ' + b;
   int ret = 0;
   reps(i, 0, alen)dp[0][i] = 0;
   reps(i, 0, blen)dp[i][0] = 0;

   reps(i, 1, alen){
      reps(j, 1, blen){
         if(a[i] == b[j]){
            dp[i][j] = dp[i-1][j-1] + 1;
         } else {
            dp[i][j] = max(dp[i-1][j] , dp[i][j-1] );
         }
         chmax(ret, dp[i][j]);
      }
   }
   return(ret);
}

signed main()
{
   FAST();
   int N;
   cin >> N;
   rep(i, N){
      string a,b;
      cin >> a >> b;
      outl( slv(a, b) );
   }
}

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

using namespace std;

#define Vi vector<int>
#define FOR(i,s,e) for (int i=s; i<e; i++)
#define ITER(v) v.begin(), v.end()

constexpr int MAX_LEN = 1'000;
int dp[MAX_LEN][MAX_LEN];

void set_val(int i, int j, int inc) {
    int val = inc;
    if (i > 0 && j > 0) {
        int tmp = dp[i-1][j-1] + inc;
        val = max(val, tmp);
    }
    if (i > 0) {
        int tmp = dp[i-1][j] + inc;
        val = max(val, tmp);
    }
    if (j > 0) {
        int tmp = dp[i][j-1] + inc;
        val = max(val, tmp);
    }
    dp[i][j] = val;
}

int lcs(const string& x, const string& y) {
    int lx = x.length();
    int ly = y.length();
    for (int i=0; i<lx; ++i) {
        bool isalreadyinc = false;
        for (int j=0; j<ly; ++j) {
            if (x[i] == y[j] && !isalreadyinc) {
                set_val(i, j, 1);
                isalreadyinc = true;
            } else {
                set_val(i, j, 0);
            }
        }
    }
    return dp[lx-1][ly-1];
}

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

    int n;
    cin >> n;

    string x, y;
    FOR(i,0,n) {
        cin >> x;
        cin >> y;
        cout << lcs(x, y) << endl;
    }

    return 0;
}

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

#define MAX 1001

using namespace std;

int lcs(string X, string Y);

int main()
{
	int n;
	cin >> n;
	string X, Y;
	for (int i = 0; i < n; i++)
	{
		cin >> X >> Y;
		cout << lcs(X, Y) << endl; 
	}
}


int lcs(string X, string Y)
{
	int c[MAX][MAX] = {0};
	X = ' ' + X;
	Y = ' ' + Y;
	for (int i = 1; i < X.length(); i++)
	{
		for (int j = 1; j < Y.length(); j++)
		{
			if (X[i] == Y[j])
				c[i][j] = c[i-1][j-1] + 1;
			else
				c[i][j] = max(c[i-1][j], c[i][j-1]);
		}
	}
	return c[X.length()-1][Y.length()-1];
}

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

static const int N = 1000;

int lcs(string X, string Y){
    int c[N + 1][N + 1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;
    Y = ' ' + Y;
    for (int i = 0; i <= m; ++i){
        c[i][0] = 0;
    }
    for (int i = 0; i <= n; ++i)
    {
        c[0][i] = 0;
    }
    for (int i = 1; i <= m; ++i)
    {
        for (int j = 1; j <= n; ++j){
            if( X[i] == Y[j]){
                c[i][j] = c[i - 1][j - 1] + 1;
            }else{
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main()
{
    string s1, s2;
    int n;
    cin >> n;

    for (int i = 0; i < n; i++){
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }

        return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
static const int N=1000;


int lcs(string X,string Y){
    int c[N+1][N+1];
    int m = X.size();
    int n = Y.size();
    int maxl = 0;
    X = ' ' + X;//在X[0]中插入空格
    Y = ' ' + Y;//在Y[0]中插入空格
    for(int i=0;i<=m;i++) c[i][0]=0;
    for(int j=1;j<=n;j++) c[0][j]=0;
    
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            if(X[i]==Y[j]) c[i][j]=c[i-1][j-1]+1;
            else c[i][j]=max(c[i-1][j],c[i][j-1]);
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main(){
    string s1,s2;
    int n;
    cin>>n;
    for(int i=0; i<n; i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    return 0;
}

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

int main(){
  int i,j,n,c[1001][1001];
  string x,y;
  cin >> n;
  while(n>0){
    n--;
    cin >> x;
    cin >> y;

    int lenx = x.size();
    int leny = y.size();

    for(i = 1; i <= lenx; i++){
      c[i][0] = 0;
    }
    for(j = 1; j <= leny; j++){
      c[0][j] = 0;
    }
  
    for(i = 1; i <= lenx; i++){
      for(j = 1; j <= leny; j++){
	if(x[i-1]==y[j-1]){
	  c[i][j] = c[i-1][j-1] + 1;
	}else if(c[i-1][j] >= c[i][j-1]){
	  c[i][j] = c[i-1][j];
	}else{
	  c[i][j] = c[i][j-1];
	}
      }
    }
    cout << c[lenx][leny] << endl;
  }
  return 0;
}

C++: #include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define MAX 1000



int solve(string x, string y) {
  int xl = x.size();
  int yl = y.size();
  int lcs[MAX + 1][MAX + 1];

  int i, j;
  for (i = 0; i < xl; i++)
    lcs[i][0] = 0;
  for (j = 0; j < yl; j++)
    lcs[0][j] = 0;

  // shift
  x = " " + x;
  y = " " + y;

  int maxlcs = 0;
  for (i = 1; i <= xl; i++) {
    for (j = 1; j <= yl; j++) {
      if (x[i] == y[j]) {
        lcs[i][j] = lcs[i - 1][j - 1] + 1;
      } else {
        int a = lcs[i][j - 1];
        int b = lcs[i - 1][j];
        lcs[i][j] = max(a, b);
      }
      maxlcs = max(maxlcs, lcs[i][j]);
    }
  }
  return maxlcs;
}

int main() {
  string s1, s2;
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> s1;
    cin >> s2;
    cout << solve(s1, s2) << endl;
  }
}