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>

using namespace std;
const int L = 1001;
const int INF = -1001001001;

int main()
{
    string s, t;
    int dp[L][L];
    int q;
    cin >> q;
    for(int i=0;i<q;++i)
    {
        cin >> s;
        cin >> t;
        for(int j=0;j<s.size()+1;++j) dp[j][0] = 0;
        for(int j=0;j<t.size()+1;++j) dp[0][j] = 0;
        for(int j=1;j<s.size()+1;++j)
        {
            for(int k=1;k<t.size()+1;++k)
            {
                if(s[j-1] == t[k-1])
                {
                    dp[j][k] = max(max(dp[j][k-1], dp[j-1][k-1] + 1), dp[j-1][k]);
                }
                else
                {
                    dp[j][k] = max(dp[j][k-1], dp[j-1][k]);
                }
            }
        }
        cout << dp[s.size()][t.size()] << endl;
    }
}

C++: #include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <array>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = a; i < (n); i++)
#define all(in) in.begin(), in.end()
const int INF = 1e9;
const int MOD = 1e9 + 7;
#define MAX_q 150
#define MAX_L 1000
string X, Y;
int dp[MAX_L + 1][MAX_L + 1];
void DP() {
    rep(i, X.size()) {
        rep(k, Y.size()) { dp[i][k] = 0; }
    }
    rep(i, X.size()) {
        rep(k, Y.size()) {
            if (X[i] == Y[k]) {
                dp[i + 1][k + 1] = dp[i][k] + 1;
            } else {
                dp[i + 1][k + 1] = max(dp[i][k + 1], dp[i + 1][k]);
            }
        }
    }
}
int main() {
    int q;
    cin >> q;
    rep(j, q) {
        cin >> X >> Y;
        DP();
        cout << dp[X.size()][Y.size()] << endl;
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[1001][1001];




	
int main (){
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	//input
	int n;
	cin >> n;
	vector<string> S(n);
	vector<string> T(n);
	for(int i=0;i<n;i++)cin >> S[i] >> T[i];


	//alorithm
	for(int i=0;i<n;i++){
		string s,t;
		s=S[i];
		t=T[i];
		memset(dp,0,sizeof(dp));
		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;continue;}
				dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
			}
		}
		cout << dp[s.size()][t.size()] << endl;
	}
}

C++: #include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;

void print() { cout << endl; }
template <typename Head, typename... Tail>
void print(Head head, Tail... tail) {
    int size = sizeof...(Tail);
    cout << head;
    if (size > 0) {
        cout << " ";
    }
    print(tail...);
}

void print0() {}
template <typename Head, typename... Tail>
void print0(Head head, Tail... tail) {
    cout << head;
    print0(tail...);
}

struct xstruct {
    ll val;
};
bool operator<=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val <= rhs.val;
}
bool operator<(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val < rhs.val;
}
bool operator>=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val >= rhs.val;
}
bool operator>(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val > rhs.val;
}
bool operator==(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val == rhs.val;
}
bool operator!=(const xstruct& lhs, const xstruct& rhs) {
    return lhs.val != rhs.val;
}
ostream& operator<<(ostream& os, xstruct const& xst) {
    return os << xst.val;
}

ll recursive(ll a, ll b, string& sa, string& sb, vector<vector<ll>>& dp) {
    ll maxval = 0;
    if (a == sa.length() || b == sb.length()) {
        return maxval;
    }
    if (dp[a][b] >= 0) {
        return dp[a][b];
    }

    if (sa.at(a) == sb.at(b)) {
        maxval = max(maxval, 1 + recursive(a + 1, b + 1, sa, sb, dp));
    }
    maxval = max(maxval, recursive(a + 1, b, sa, sb, dp));
    maxval = max(maxval, recursive(a, b + 1, sa, sb, dp));
    dp[a][b] = maxval;
    return dp[a][b];
}

int main() {
    ll N = 0;
    cin >> N;
    string nil;
    getline(cin, nil);
    for (ll iter = 0; iter < N; iter++) {
        string sa, sb;
        getline(cin, sa);
        getline(cin, sb);
        ll la = sa.length();
        ll lb = sb.length();

        vector<vector<ll>> dp(la, vector<ll>(lb));
        for (ll a = 0; a < la; a++) {
            for (ll b = 0; b < lb; b++) {
                dp[a][b] = -1;
            }
        }
        recursive(0, 0, sa, sb, dp);
        print(dp[0][0]);
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <queue>
#include <numeric>
#include <climits>
using namespace std;
//using ll = long long;
//const ll mod = 1000000007;
int n, m;
string s, t;
const int n_max = 1000;
const int m_max = 1000;

int main() {
	cin >> n;
	for(int k=0; k<n; ++k){
        cin >> s >> t;
        int dp[n_max+1][m_max+1] = {0};
	for (int i = 0; i < s.size(); ++i) {
		for (int j = 0; j < t.size(); ++j) {
			if (s[i] == t[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[s.size()][t.size()] << endl;
    }
	

	return 0;
}