Go: package main

import (
	"fmt"
	"os"
	"strconv"
	"bufio"
	"strings"
	"sort"
)

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

C++: //This template was last modified on 2/1/2020.
//Define many functions and words.
//The functions I have defined start with a capital letter.
#include <bits/stdc++.h>
using namespace std;
//C++ words.

#define MOD 1000000007
//#define MOD 998244353

typedef long long ll;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<vl> matrix;//A(n, vl(m));
typedef vector<string> vs;
typedef pair<ll,ll> pl;
typedef vector<pl> vp;
typedef pair<double,double> pd;
typedef vector<pd> vpd;
typedef vector<bool> vb;
typedef map<ll,ll> mapl;
typedef priority_queue<ll> pque;
#define nl "\n"s
#define rep(i,n) for(ll i=0LL; i<(n); i++)
#define INF 1001001001001001001
#define PI 3.14159265358979323846
#define SP << " "s <<
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define All(V) V.begin(),V.end()
#define Sortup(V) sort(All(V))
#define Sortdown(V) sort(V.rbegin(), V.rend())
#define Atleast(key, V) lower_bound(All(V), key)
#define Largerthan(key, V) upper_bound(All(V), key)

ll IN(){ll x; cin>>x; return x;}
void VIN(vl &x){rep(i, x.size()) cin>>x[i];}
#define endYes do{cout << "Yes" << nl; return 0;}while(0)
#define endYES do{cout << "YES" << nl; return 0;}while(0)
#define endNo  do{cout << "No" << nl; return 0;}while(0)
#define endNO  do{cout << "NO" << nl; return 0;}while(0)
#define endS(str) do{cout << str << nl; return 0;}while(0)


//Mathmatical functions.

template<class T>bool Chmax(T &a, T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool Chmin(T &a, T b){if(a>b){a=b;return 1;}return 0;}
template<class T>double Dist(T x,T y,T X,T Y){return sqrt( (x-X)*(x-X)+(y-Y)*(y-Y) );}
ll Digit(ll N){ll res=0;while(N) res++, N/=10;return res;}
bool Bit(ll subset, ll Rsift){ return ((subset)>>(Rsift))&1; }
ll Gcd(ll a,ll b){ return b?Gcd(b,a%b):abs(a); }
ll Lcm(ll A,ll B){ return A/Gcd(A,B)*B; }
ll Lpow(ll e,ll x){ll res=1LL;while(x){if(x&1)res*=e;e*=e;x/=2;}return res;}
ll Pow(ll e,ll x){ll res=1LL;while(x){if(x&1)res=(1LL*res*e)%MOD;e=(1LL*e*e)%MOD;x/=2;}return res;}
void Mul(ll &a,ll b){ a=1LL*a*b%MOD; }
void CumuSum(vl &A){rep(i,A.size())if(i)A[i]+=A[i-1];}
void Eratos_prime(vb &is_prime){//1-indexed
    ll N=is_prime.size()-1;
    rep(i, N) is_prime.at(i) = true;
    is_prime.at(0) = is_prime.at(1) = false;
    for(ll i=2; i*i<=N; i++) if(is_prime.at(i)) {
        for(ll j=i+i; j<=N; j+=i) is_prime.at(j) = false;
    }
}
void Primefact(ll N, mapl &elem){
    for(ll i=2; i*i<=N; i++) {
        while(N%i==0) {
            elem[i]++;
            N /= i;
        }
    }
    if(N>1) elem[N]++;
}


/*
//Topological sort  O(|V|+|E|)
vl g[100100];
vb used(100100);
vl Sortedgraph;

void dfs_Tsort(ll u) {
  if(used[u]) return;
  used[u] = true;
  for(auto x: g[u]) dfs_Tsort(x);
  Sortedgraph.push_back(u);
}

//N:=|V|
void Topo_sort(ll N) {
  rep(i, N) dfs_Tsort(i);
  reverse(All(Sortedgraph));
}
*/

/*
//Warshall floyd  O(N**3)
#define MAX_V 100
matrix d(MAX_V, vl(MAX_V));

void warshall_floyd(ll V){
    rep(k, V) rep(i, V) rep(j, V){
        Chmin(d[i][j], d[i][k] + d[k][j]);
    }
}
*/

/*
//Combination in MOD  O(N)
//Constraints 1<=k<=n<10^7  and  MOD is prime  and MOD>n
const ll MAX=1001001;
vl fac(MAX), finv(MAX), inv(MAX);
void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (ll i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
//Combination
ll COM(ll n, ll k){
    if (n<k or n<0 or k<0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
*/

//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
// solution comes here

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

    ll N; cin >> N;
    while(N--){
        string s, t; cin >> s >> t;
        matrix dp(s.size()+1, vl(t.size()+1, 0));
        rep(i, s.size()){
            rep(j, t.size()){
                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()] << nl;
    }
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define ll long long
using namespace std;

int dp[1000+1][1000+1];//dp[i][j]:=Xのi番目までの文字列とYのj番目までの文字列のLCS

int solve(string X,string Y){
    int m=X.size();
    int n=Y.size();

    rep(i,m) dp[i][0]=0;
    rep(j,n) dp[0][j]=0;

    X=' '+X;
    Y=' '+Y;

    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]);
            }
        }
    return dp[m][n] ;
}

int main(){
    int n;
    cin>>n;

    string X,Y;

    rep(i,n){
        cin>>X;
        cin>>Y;
        cout<<solve(X,Y)<<endl;
    }
    return 0;
}

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

int main(){
    int q;
    cin >> q;
    for(int k = 0; k < q; k++){
        string s1, s2;
        cin >> s1;
        cin >> s2;
        int arr[s1.length()+1][s2.length()+1];
        for(int i = 0; i <= s1.length(); i++){
            for(int j = 0; j <= s2.length(); j++){
                if(i == 0 || j == 0){
                    arr[i][j] = 0;
                }else{
                    if(s1[i-1] == s2[j-1])
                        arr[i][j] = arr[i-1][j-1] + 1;
                    else
                        arr[i][j] = max(arr[i-1][j],arr[i][j-1]);
                }
            }
        }
        //debug
        /*
        for(int j = 0; j <= s2.length(); j++){
            for(int i = 0; i <= s1.length(); i++){
                cout << arr[i][j];
            }
            cout << endl;
        }
        */
        cout << arr[s1.length()][s2.length()] << endl;
    }
}