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++: #include <bits/stdc++.h>
using namespace std;

template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }

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

	int q;
	cin >> q;

	while(q--) {
		string a, b;
		cin >> a >> b;

		const int n = a.size();
		const int m = b.size();

		vector<vector<int>> dp(n + 2, vector<int>(m + 2, 0));
		for(int i = 0; i <= n; ++i) {
			for(int j = 0; j <= m; ++j) {
				if(a[i] == b[j]) {
					chmax(dp[i + 1][j + 1], dp[i][j] + 1);
				}
				else {
					chmax(dp[i + 1][j + 1], dp[i][j]);
					chmax(dp[i + 1][j], dp[i][j]);
					chmax(dp[i][j + 1], dp[i][j]);
				}
			}
		}

		cout << dp[n][m] << endl;
	}

	return EXIT_SUCCESS;
}

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

constexpr int MAX_XY = 1010;

int lx, ly, q, dp[MAX_XY][MAX_XY];
std::string sx, sy;

void fill_dp(){
    for(int i = 1; i <= lx; ++i){
        for(int j = 1; j <= ly; ++j){
            if(sx[i - 1] == sy[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
            else dp[i][j] = std::max(dp[i - 1][j], dp[i][j - 1]);
        }
    }
}

void solve(){
    fill_dp();
    std::cout << dp[lx][ly] << std::endl;
    return;
}

int main(){
    std::cin >> q;
    
    for(int i = 0; i < q; ++i){
        std::cin >> sx >> sy;
        lx = sx.size(), ly = sy.size();
        solve();
    }
    return 0;
}

C++: #include <cassert>

#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
// #include <boost/foreach.hpp>
// #include <boost/range/algorithm.hpp>
#define rep(i,j,k) for(int i=(int)j;i<(int)k;i++)
#define ll long long
#define Sort(v) sort(all(v))
//#define INF 1e9
#define LINF 1e18
#define END return 0
#define pb push_back
#define se second
#define fi first
#define pb push_back
#define all(v) (v).begin() , (v).end()
#define MP make_pair
#define int long long
using namespace std;
int day[12]={31,28,31,30,31,30,31,31,30,31,30,31};
// int dx[]={0,1,0,-1};
// int dy[]={1,0,-1,0};
struct edge{int to,cost;};
//typedef pair<int,int> P;

const long long MOD=1000000007LL;
bool isupper(char c){if('A'<=c&&c<='Z')return 1;return 0;}
bool islower(char c){if('a'<=c&&c<='z')return 1;return 0;}
bool isPrime(int x){if(x==1)return 0;if(x==2)return 1;if(x%2==0)return 0;for(int i=3;i*i<=x;i++)if(x%i==0)return 0;return 1;}
bool iskaibun(string s){for(int i=0;i<s.size()/2;i++)if(s[i]!=s[s.size()-i-1])return 0;return 1;}
bool isnumber(char c){return ('0'<=c&&c<='9');}
bool isalpha(char c){return (isupper(c)||islower(c));}
template<typename T> 
void print(vector<T> v){
    for(int i=0;i<v.size();i++){
        if(i)cout<<" ";
        cout<<v[i];
    }
    cout<<endl;
}
int INF=1e18;

template<typename T>
void printendl(vector<T> v){
    for(auto date:v)cout<<date<<endl;
}

template<typename T>
void printvv(vector<vector<T>> v){
    for(int i=0;i<v.size();i++){
        for(int j=0;j<v[i].size();j++){
            if(j)cout<<" ";
            cout<<v[i][j];
        }
        cout<<endl;
    }
}
int gcd(int a,int b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    else return gcd(b,a%b);
}

struct Edge{
    int to,cost;
};

int LCS(string s, string t){
    int n=s.size();
    int m=t.size();
    vector<vector<int>> dp(n+1,vector<int>(m+1, 0));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(s[i-1]==t[j-1])dp[i][j]=dp[i-1][j-1]+1;
            else dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
        }
    }
    return dp[n][m];
}

signed main (){
    int n;cin>>n;
    rep(i,0,n){
        string s,t;
        cin>>s>>t;
        cout<<LCS(s,t)<<endl;
    }

}
/*

*/

C++: #include<stdio.h>
#include<string.h>
void Length(void);

char A[1001], B[1001];

int a[1001][1001];

int n, m;

main(){
  int i, q;

  scanf("%d",&q);

  for(i = 0; i < q; i++){

    scanf("%s", A);

    scanf("%s", B);

    Length();

    printf("%d\n", a[m][n]);
  }

  return 0;
}

void Length(void){

  int i, j;

  m = strlen(A);

  n = strlen(B);

  for(i = 1; i <= m; i++){

    a[i][0] = 0;

  }

  for(j = 0; j <= n; j++){

    a[0][j] = 0;

  }

  for(i = 1; i <= m; i++){
    for(j = 1; j <= n; j++){

      if(A[i-1] == B[j-1])
      a[i][j] = a[i - 1][j - 1] + 1;



      else  if(a[i - 1][j] >= a[i][j-1])

	a[i][j] = a[i - 1][j];

      else a[i][j] = a[i][j -1];
    }
  }
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>

#define SIZE 1002
int Table[SIZE][SIZE];
char a[SIZE];
char b[SIZE];
int n1;
int n2;

int Count(int i, int j)
{
    if(i == 0 || j == 0) return 0;
    if(a[i-1] == b[j-1]){
        return Table[i-1][j-1] + 1;
    }
    return std::max(Table[i-1][j], Table[i][j-1]);
}
int Solve()
{
    n1 = std::strlen(a);
    n2 = std::strlen(b);
    for(std::size_t i = 1; i < n1; ++i){
        for(std::size_t j = 1; j < n2; ++j){
            Table[i][j] = Count(i, j);
        }
    }
    return Table[n1-1][n2-1];
    
}

int main()
{
    int n;
    std::scanf("%d\n", &n);
    for(int i = 0; i < n; ++i){
        std::fgets(a, SIZE, stdin);
        std::fgets(b, SIZE, stdin);
        std::printf("%d\n", Solve());
    }
    return 0;
}