Go: package main

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

const N = 1001

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

func lcs(x, y string) int {
	var c [N][N]int
	m := len(x)
	n := len(y)
	var maxl int

	x = " " + x
	y = " " + y

	for i := 1; i <= m; i++ {
		for 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
}

func answer(reader io.Reader) string {
	sc := bufio.NewScanner(reader)
	sc.Split(bufio.ScanWords)

	sc.Scan()
	q, _ := strconv.Atoi(sc.Text())

	var out bytes.Buffer
	for i := 0; i < q; i++ {
		sc.Scan()
		s1 := sc.Text()
		sc.Scan()
		s2 := sc.Text()
		out.WriteString(fmt.Sprintf("%d\n", lcs(s1, s2)))
	}
	return strings.TrimRight(out.String(), "\n")
}

func main() {
	fmt.Println(answer(os.Stdin))
}

C++: /*
??????-???_
  -????????????=- ???
  ''???-???"''-=?????????=- ???_
          "'rfi??????-???"''-?????????=-???_
           .i?????????i=??? "''???-???"''-?????????=-???_
           i?????????i.??????=- .   "'???-???"''-?????????-???_
          .i???=???i"'???-"_-=?????????=- .  "'???-???"''-?????????-???_
          i?????????i.         " ???"_-=?????????=-.,.??¨rfi??????-???"???|
         .i?????????i                  "  ???"  -???i??????i "'???-''???
         i??????=i                             "i??????i
        .i?????????i                             i??????i
        i?????????i                              i??????i
       .i?????????i                               i??????i
       i?????????i                               i??????i
      .i?????????i                               i???=i
     i????????????i                                 i??????i
     i????????????i                                 i??????i
     i????????????i                                i??????i
     i????????????i                                 i??????i
    i????????????i                                  i??????i
    i????????????i                                  i=??????i
   i????????????i                                  i?????????i
   i???????????????i                                  ?¨"""?¨
   ???????????????                         
*/

#include "bits/stdc++.h"
#include <unordered_set>

#define _CRT_SECURE_NO_WARNINGS

#define REP(i, n) for(decltype(n) i = 0; i < (n); i++)
#define REP2(i, x, n) for(decltype(x) i = (x); i < (n); i++)
#define REP3(i, x, n) for(decltype(x) i = (x); i <= (n); i++)
#define RREP(i, n) for (decltype(n) i = (n) - 1; i >= 0; i--)

#define ALL(a) (a).begin(),(a).end()
#define SORT(c) sort((c).begin(),(c).end())
#define DESCSORT(c) sort(c.begin(), c.end(), greater<int>())

using namespace std;

const int INF = 1000000000;
const double PI = 3.1415926535897932384626433832795;

// N, E, S, W
const int dx[4] = { -1, 0,  1,  0 };
const int dy[4] = { 0, 1,  0, -1 };

using LL = long long int;
using LD = long double;
using pii = pair<int, int>;
using pdd = pair<double, double>;
using pll = pair<LL, LL>;
using vi = vector<int>;
using vd = vector<double>;
using vvi = vector<vi>;
using vll = vector<LL>;

//==============================================
vvi dp(1050, vi(1050));

int lcs(string x, string y) {
	int ans = 0;
	int m = x.length();
	int n = y.length();

	x = ' ' + x;
	y = ' ' + y;

	REP3(i, 1, m) {
		REP3(j, 1, n) {
			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];
			}

			ans = max(ans, dp[i][j]);
		}
	}

	return ans;
}

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

	int q;
	cin >> q;

	string str1, str2;
	REP(i, q) {
		cin >> str1 >> str2;
		cout << lcs(str1, str2) << "\n";
	}
	return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <utility>
#include <tuple>
#include <algorithm>
#include <numeric>
#include <cstdio>
#include <cmath>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int main(){
  int q;
  cin >> q;
  rep(repert, q) {
    string X, Y;
    cin >> X >> Y;
    vector<vector<int>> dp(X.size()+1, vector<int>(Y.size()+1));
    rep(i, X.size()) {
      rep(j, Y.size()) {
        if(X[i] == Y[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[X.size()][Y.size()] << endl;
  }
  return 0;
}

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

int DP[1010][1010];

int main() {
  int n;
  cin >> n;
  for (auto t=0; t<n; t++) {
    string A, B;
    cin >> A >> B;
    int LA = (int) A.size();
    int LB = (int) B.size();
    fill(&DP[0][0], &DP[0][0]+1010*1010, 0);
    for (auto i=-1; i<LA; i++) {
      for (auto j=-1; j<LB; j++) {
        if (i == -1 || j == -1) {
          DP[i+1][j+1] = 0;
        } else if (A[i] == B[j]) {
          DP[i+1][j+1] = 1 + DP[i][j];
        } else {
          DP[i+1][j+1] = max(DP[i+1][j], DP[i][j+1]);
        }
      }
    }
    cout << DP[LA][LB] << endl;
  }
}