Go: package main

import "fmt"

func initialize_grid(width int,height int)[][]int{
	var grid [][]int
	for i:=0;i<height+1;i++{
		var grid_row  []int
		for j:=0;j<width+1;j++{
			grid_row = append(grid_row,0)
		}
		grid = append(grid,grid_row[:])
	}
	return grid
}

func ma(x int,y int)int{
	if x >= y{
		return x
	}else{
		return y
	}
}

func main()  {
	//Take input
	var num int
	var st1,st2 string
	var string_pairs [][]string
	fmt.Scan(&num)
	for i:=0;i<num;i++{
		var string_pair []string
		fmt.Scan(&st1)
		fmt.Scan(&st2)
		string_pair = append(string_pair,st1,st2)
		string_pairs = append(string_pairs,string_pair)
	}
	//DP
	//initialize grid
	for i:=0;i<num;i++{
		string_1 := string_pairs[i][0]
		string_2 := string_pairs[i][1]

		dp := initialize_grid(len(string_2),len(string_1))

		for j:=1;j<len(string_1)+1;j++{
			for k:=1;k<len(string_2)+1;k++{
				if string_1[j-1] == string_2[k-1]{
					dp[j][k] = dp[j-1][k-1] + 1
				}else{
					dp[j][k] = ma(dp[j-1][k],dp[j][k-1])
				}
			}
		}
		fmt.Println(dp[len(string_1)][len(string_2)])
	}
}

C++: #ifdef LOCAL
#pragma GCC optimize ("O0")
#else
#pragma GCC optimize ("O3")
#endif


#include <bits/stdc++.h>


using namespace std;

using ll = long long;
const int INF = 1e9 + 7;
const int MOD = INF;
const ll INF_LL = 1e18 + 7;

#define __overload3(_1, _2, _3, name,...) name
#define rep(...) __overload3(__VA_ARGS__, repFromUntil, repUntil, repeat)(__VA_ARGS__)
#define repeat(times) repFromUntil(__name, 0, times)
#define repUntil(name, times) repFromUntil(name, 0, times)
#define repFromUntil(name, from, until) for (int name = from, name##__until = (until); name < name##__until; name++)
#define repFromTo(name, from, to) repFromUntil(name, from, to + 1)
#define repr(...) __overload3(__VA_ARGS__, reprFromUntil, reprUntil, repeat)(__VA_ARGS__)
#define reprUntil(name, times) reprFromUntil(name, 0, times)
#define reprFromUntil(name, from, until) for (int name = until - 1, name##__from = (from); name >= name##__from; name--)
#define reprFromTo(name, from, to) reprFromUntil(name, from, to + 1)

#define debugos OUT
#ifdef LOCAL
#define debug(v) {printf("L%d %s > ",__LINE__,#v);debugos<<(v)<<newl;}
#define debugv(v) {printf("L%d %s > ",__LINE__,#v);for(auto e:(v)){debugos<<e<<" ";}debugos<<newl;}
#define debuga(m,w) {printf("L%d %s > ",__LINE__,#m);for(int x=0;x<(w);x++){debugos<<(m)[x]<<" ";}debugos<<newl;}
#define debugaa(m,h,w) {printf("L%d %s >\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){debugos<<(m)[y][x]<<" ";}debugos<<newl;}}
#else
#define debug(v) {}
#define debugv(v) {}
#define debuga(m,w) {}
#define debugaa(m,h,w) {}
#endif

#define newl "\n"

#define all(iter) begin(iter), end(iter)


template <class T> bool chmin(T& var, T x) {
  if (var > x) {
    var = x;
    return true;
  } else return false;
}
template <class T> bool chmax(T& var, T x) {
  if (var < x) {
    var = x;
    return true;
  } else return false;
}

class MyScanner {
  public:
    template<typename T> void input_integer(T& var) {
        var = 0; T sign = 1;
        int cc = getchar();
        for (; cc<'0' || '9'<cc; cc = getchar())
            if (cc == '-') sign = -1;
        for (; '0' <= cc && cc <= '9'; cc = getchar())
            var = (var << 3) + (var << 1) + cc - '0';
        var = var * sign;
    }
    int c() { char c; while (c = getchar(), c == ' ' or c == '\n'); return c; }
    MyScanner& operator>>(char& var) { var = c(); return *this; }
    MyScanner& operator>>(int& var) { input_integer<int>(var); return *this; }
    MyScanner& operator>>(ll& var) { input_integer<ll>(var); return *this; }
    MyScanner& operator>>(string& var) {
        int cc = getchar();
        for (; !isvisiblechar(cc); cc = getchar());
        for (; isvisiblechar(cc); cc = getchar())
            var.push_back(cc);
        return *this;
    }
    operator int() {
      int n;
      *this >> n;
      return n;
    }
    operator ll() {
      ll n;
      *this >> n;
      return n;
    }
    private:
      int isvisiblechar(int c) {
        return 0x21 <= c && c <= 0x7E;
      }
};
class MyPrinter {
  public:
    template<typename T>
    void output_integer(T var) {
        if (var == 0) { putchar('0'); return; }
        if (var < 0)
            putchar('-'),
            var = -var;
        char stack[32]; int stack_p = 0;
        while (var)
            stack[stack_p++] = '0' + (var % 10),
            var /= 10;
        while (stack_p)
            putchar(stack[--stack_p]);
    }
    MyPrinter& operator<<(char c) { putchar(c); return *this; }
    template <typename T>
    MyPrinter& operator<<(T var) { output_integer<T>(var); return *this; }
    MyPrinter& operator<<(char* str_p) { while (*str_p) putchar(*(str_p++)); return *this; }
    MyPrinter& operator<<(const char* str_p) { while (*str_p) putchar(*(str_p++)); return *this; }
    MyPrinter& operator<<(const string& str) {
        const char* p = str.c_str();
        const char* l = p + str.size();
        while (p < l) putchar(*p++);
        return *this;
    }
    // MyPrinter& operator<<(const modint& var) { output_integer<ll>(var.value); return *this; }
    template <typename T>
    void operator()(T x) {
        *this << x << newl;
    }
};
MyScanner IN;
MyPrinter OUT;


int main() {
  int q = IN;
  string x, y;
  rep(q) {
    x = y = "";
    IN >> x >> y;
    int s = x.length(), t = y.length();
    int dp[s+2][t+2];
    rep(i, s+2) dp[i][0] = 0;
    rep(i, t+2) dp[0][i] = 0;
    rep(i, s+1) {
      rep(j, t+1) {
        dp[i+1][j+1] = max(dp[i+1][j], dp[i][j+1]);
         if (x[i] == y[j]) chmax(dp[i+1][j+1], dp[i][j] + 1);
      }
    }
    OUT(dp[s][t]);
  }
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <map>
#include <numeric>
#include <string>
#include <cmath>
#include <iomanip>
#include <string>
#include <queue>
#include <list>
#include <stack>

typedef long long ll;
using namespace std;
const int mod = 1000000007;
const int MAX = 20*200+1;

int LCS(string S, string T);

int main() {

    int n; cin >> n;
    vector<int> ans(n);
    for (int i = 0; i < n; i++) {
        string s, t;
        cin >> s >> t;
        ans[i] = LCS(s, t);
    }

    for (int i = 0; i < n; i++) {
        cout << ans[i] << '\n';
    }

}

int LCS(string S, string T) {

    int row = S.size();
    int column = T.size();
    vector< vector<int> > dp(row, vector<int>(column, 0));

    for (int i = 0; i < row; i++) {
        if(S[i] == T[0]) {
            dp[i][0] = 1;
        }
        if (i > 0) {
            if (dp[i-1][0])
                dp[i][0] = 1;
        }
    }
    for (int i = 0; i < column; i++) {
        if(S[0] == T[i]) {
            dp[0][i] = 1;
        }
        if (i > 0) {
            if (dp[0][i-1])
                dp[0][i] = 1;
        }
    }

    for (int i = 1; i < row; i++) {
        for (int j = 1; j < column; j++) {
            if (S[i] == T[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[row-1][column-1];

}