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++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(v) (v).begin(), (v).end()
#ifdef DBGPRT
#define dbg(n) cerr << "//" << #n << ":" << n <<endl
#else
#define dbg(n) ;
#endif
typedef pair<ll, ll> LLARR;
#define INF 999999999
const double PI = 3.1415926535897932384626433832795;

ll lcs(string s1, string s2)
{
	int m = len(s1);
	int n = len(s2);
	s1 = ' '+s1;
	s2 = ' '+s2;
	int (*mat)[1001];
	mat = new int[1001][1001];
	REP(i, m)
	{
		mat[i][0] = 0;
	}
	REP(i, n)
	{
		mat[0][i] = 0;
	}
	int maxx = 0;
	FOR(i, 1, m+1)
	{
		FOR(j, 1, n+1)
		{
			if (s1[i] == s2[j])
				mat[i][j] = mat[i-1][j-1] + 1;
			else
				mat[i][j] = max(mat[i][j-1], mat[i-1][j]);
			maxx = max(maxx, mat[i][j]);
		}
	}
	delete[] mat;
	return maxx;
}
int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	REP(i, n)
	{
		string s1;
		cin >> s1;
		string s2;
		cin >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}

C++: #include <iostream> 
#include <cstring> 
using namespace std;
int c[1001][1001];

int lcs(string x, string y)
{
    int maxl = 0;
    int m = x.length(), n = y.length();
 
    for (int i = 1; i <= m; i++) c[i][0] = 0;
    for (int j = 1; j <= n; j++) c[0][j] = 0;


    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (x[i] == y[j])
            { 
                if(i==0 || j==0) c[i][j] = 1;
                else c[i][j] = c[i-1][j-1] + 1; 
            }else
            {
                if(i==0 || j==0) c[i][j] = 0;
                else c[i][j] = max(c[i][j-1], c[i-1][j]);
            }
            maxl = max(maxl, c[i][j]);
        } 
    } 
    return maxl;
}

int main()
{
    
    //freopen("in1.txt","r",stdin);
    string x,y;
    int q;
    cin>>q;
    while(q--)
    {
        cin>>x>>y;
        int ans = lcs(x, y);
        cout<<ans<<endl;
    }
    return 0;
}

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

using std::cin;
using std::cout;
using std::cerr;
using std::endl;

// 最長共通部分列 ... len(s) == m, len(t) == n とすると, O(mn)
int lcs(std::string s, std::string t) {
  std::vector<std::vector<int> > dp(s.size() + 1, std::vector<int>(t.size() + 1, 0));

  for (int i = 1; i <= s.size(); ++i) {
    for (int j = 1; j <= t.size(); ++j) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      else if (dp[i - 1][j] >= dp[j][i - 1]) {
        dp[i][j] = dp[i - 1][j];
      }
      else {
        dp[i][j] = dp[i][j - 1];
      }
    }
  }

  return dp[s.size()][t.size()];
}

int main() {
  std::string s, t;

  while (cin >> s >> t) {
    cout << lcs(s, t) << endl;
  }

  return 0;
}

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

int L[1010][1010]={};
string s,t;
int o,p;

int main(){
  int N;
  cin >> N;
  for(int n=0;n<N;n++){
    cin >> s >> t;
    o=s.size();
    p=t.size();
    for(int A=0;A<1010;A++){
      for(int B=0;B<1010;B++){
        L[A][B]=0;
      }
    }
    for(int i=1;i<=o;i++){
      for(int j=1;j<=p;j++){
        if(s[i-1]==t[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[o][p] << endl;
  }
}

C++: ///
// File:  alds1_10_c.cpp
// Author: ymiyamoto
//
// Created on Tue Oct 17 01:36:16 2017
//

#include <cstdint>
#include <iostream>
#include <string>

using namespace std;
static uint32_t dp[1000][1000];

static void init_dp(uint32_t len1, uint32_t len2)
{
  for (uint32_t i = 0; i < len1; i++) {
    dp[i][0] = 0;
  }
  for (uint32_t i = 0; i < len2; i++) {
    dp[0][i] = 0;
  }
}

int32_t main()
{
  uint32_t n;
  cin >> n;

  for (uint32_t i = 0; i < n; i++) {
    string str1, str2;
    cin >> str1;
    cin >> str2;
    init_dp(str1.size(), str2.size());
    for (uint32_t i = 1; i <= str1.size(); i++) {
      for (uint32_t j = 1; j <= str2.size(); j++) {
        if (str1[i - 1] == str2[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[str1.size()][str2.size()] << endl;
  }

  return 0;
}

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

int lcs(string X, string Y) {
  int dp[1000 + 1][1000 + 1];
  int max = 0;
  int m = X.size();
  int n = Y.size();
  X = ' ' + X;
  Y = ' ' + Y;

  for (int i = 1; i <= m; i++)
    dp[i][0] = 0;

  for (int i = 1; i <= n; i++)
    dp[0][i] = 0;

  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      if (X[i] == Y[j])
	dp[i][j] = dp[i-1][j - 1] + 1;
      else if (dp[i - 1][j] >= dp[i][j - 1])
	dp[i][j] = dp[i - 1][j];
      else
	dp[i][j] = dp[i][j - 1];

      max = (max < dp[i][j]) ? dp[i][j] : max;
    }
  }

  return max;
}

int main(int argc, char const *argv[]) {
  int N;
  cin >> N;

  for (int i = 0; i < N; i++) {
    string X, Y;
    cin >> X >> Y;

    cout << lcs(X, Y) << endl;
  }

  return 0;
}