Go: package main

import (
	. "fmt"
    "math"
)

// func init() { Println("----------------------------------------") }

func main() {
    const L_MAX = 1003

	var q int
	Scanf("%d", &q)
	X := make([]string, 0, 150)
	Y := make([]string, 0, 150)
	for i := 0; i < 2 * q; i++ {
	    var tmp string
	    Scanf("%s", &tmp)

	    if i % 2 == 0 {
	        X = append(X, tmp)
	    } else {
	        Y = append(Y, tmp)
	    }
	}

    for i := 0; i < q; i++ {
        dp := make([][]int, L_MAX)
        for k, _ := range dp {
            dp[k] = make([]int, L_MAX)
        }

        x, y := X[i], Y[i]

        // init
        isXFound := false
        for c := 0; c < len(x); c++ {
            if isXFound || y[0] == x[c] {
                dp[0][c] = 1
                isXFound = true
            }
        }
        isYFound := false
        for r := 0; r < len(y); r++ {
            if isYFound || x[0] == y[r] {
                dp[r][0] = 1
                isYFound = true
            }
        }

        for r := 1; r < len(y); r++ {
            for c := 1; c < len(x); c++ {
                // dp[r][c]
                if x[c] == y[r] {
                    dp[r][c] = int(math.Max(float64(dp[r - 1][c - 1] + 1), float64(dp[r][c])))
                }

                dp[r][c] = int(math.Max(float64(dp[r][c]), math.Max(float64(dp[r - 1][c]), float64(dp[r][c - 1]))))
            }
        }
        Println(dp[len(y) - 1][len(x) - 1])

        // LCSも求めておく
        // ans := ""
        // r, c := len(y) - 1, len(x) - 1
        // for r >= 0 && c >= 0 {
        //     if y[r] == x[c] {
        //         ans = string(x[c]) + ans
        //         r--
        //         c--
        //     } else {
        //         if dp[r - 1][c] > dp[r][c - 1] {
        //             r--
        //         } else {
        //             c--
        //         }
        //     }
        // }
        // Println(ans)
    }
}

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 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 lcs(string X, string Y) {
  int m = X.size();
  int n = Y.size();
  int c[m+1][n+1];
  X = ' ' + X;
  Y = ' ' + Y;
  for (int i=0; i<m+1; i++) {
    c[i][0] = 0;
  }
  for (int j=0; j<n+1; j++) {
    c[0][j] = 0;
  }

  int max_lcs = 0;
  for (int i=1; i<m+1; i++) {
    for (int j=1; j<n+1; 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]);
      }
      max_lcs = max(max_lcs, c[i][j]);
    }
  }

  return max_lcs;
}


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

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

#define _For(idx, from, to, inc, cmp) \
	for(auto idx=(from) ; idx cmp (to) ; inc idx)
#define ForLE(idx, from, to) \
	_For(idx, from, to, ++, <=)
#define For(idx, from, to) \
	_For(idx, from, to, ++, <)

constexpr int MaxLen = 1000;
int Memo[MaxLen+1][MaxLen+1];
int main() {
	std::string line;
	const auto nextline = [&line](){
		std::getline(std::cin, line);
	};
	nextline();
	const int NQ = std::stoi(line);
	For(i, 0, NQ) {
		std::string s0, s1;
		std::getline(std::cin, s0);
		std::getline(std::cin, s1);

		int len0 = s0.length(),
			len1 = s1.length();
		if(len0 < len1) {
			std::swap(s0, s1);
			std::swap(len0, len1);
		}
		ForLE(j, 0, len0)
			Memo[j][0] = 0;
		ForLE(j, 0, len1)
			Memo[0][j] = 0;

		ForLE(j, 1, len1) {
			ForLE(k, 1, len0) {
				int res = std::max(
								Memo[k][j-1],
								Memo[k-1][j]
							);
				if(s0[k-1] == s1[j-1])
					res = std::max(Memo[k-1][j-1] + 1, res);
				Memo[k][j] = res;
			}
		}
		std::cout << Memo[len0][len1] << std::endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;

int main(){
	int q;cin>>q;
	for(int i = 0; i < q; i++) {
		string x,y;cin>>x>>y;
		vector<vector<int>> dp(1001,vector<int>(1001,0));
		for(int i = 0; i < x.size(); i++) {
			for(int j = 0; j < y.size(); j++) {
				if (x[i] == y[j]){
					dp[i + 1][j + 1] = max(dp[i][j] + 1,max(dp[i][j + 1],dp[i + 1][j]));
				}
				else{
					dp[i + 1][j + 1] = max(dp[i][j + 1],dp[i + 1][j]);
				}
			}
		}
		cout << dp[x.size()][y.size()] << endl;
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <array>
#include <map>
#include <iterator>
#include <algorithm>

#define X first
#define Y second

using namespace std;

int q;
vector<pair<string,string>> dataset;

int ans(pair<string, string> &data)
{
    array<array<int,1001>,1001> dp = {};
    for (unsigned int i=1; i<=data.X.length(); ++i) {
        for (unsigned int j=1; j<=data.Y.length(); ++j) {
            if (data.X[i-1] == data.Y[j-1]) dp[i][j] = max({dp[i-1][j-1] + 1, dp[i-1][j], dp[i][j-1]});
            else dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
        }
    }
    return dp[data.X.length()][data.Y.length()];
}

int main(int argc, char **argv)
{
    cin >> q;
    for (int i=0; i<q; ++i) {
        dataset.emplace_back(std::make_pair("",""));
        cin >> dataset[i].X;
        cin >> dataset[i].Y;
    }
    for (int i=0; i<q; ++i) {
        cout << ans(dataset[i]) << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int q;
  cin >> q;
  while (q--) {
    string x, y;
    cin >> x >> y;
    int m = x.length();
    int n = y.length();
    vector<vector<int>> dp(m + 1, vector<int>(n + 1, 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 {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    int ret = dp[m][n];
    cout << ret << '\n';
  }
  return 0;
}

C++: #ifdef LOCAL_BUILD
#include "pch.h"
#define DLOG(msg) cout << "#" << __LINE__ << ":" << msg << endl;
#define DLOG_V(var)\
  cout << "#" << __LINE__ << ":" << #var << " : " << var << endl;
#else
#include <bits/stdc++.h>
#define DLOG(msg)
#define DLOG_V(var)
#endif
using namespace std;

constexpr auto no_value = numeric_limits<uint64_t>::max();

// str1 should be shorter
int solve(const string& str1, const string& str2) {
  vector<int> cur(str2.size(), 0);
  vector<int> prev;
  int max_ = 0;
  for (auto it = str1.begin(); it != str1.end(); ++it) {
    prev = cur;
    for (size_t i = 0; i < str2.size(); ++i) {
      if (*it == str2[i]) {
        auto p = i == 0 ? 0 : prev[i - 1];
        cur[i] = p + 1;
        max_ = max(max_, cur[i]);
      }
    }
  }
  return max_;
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    string str1;
    string str2;
    cin >> str1 >> str2;
    auto res = (str1.size() <= str2.size()) ? solve(str1, str2) : solve(str2, str1);
    cout << res << endl;
  }
}