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

// macros
#define pb emplace_back
#define mk make_pair
#define FOR(i, a, b) for(int i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) for(int i=((int)(n)-1);i>=0;i--)
#define irep(itr, st) for(auto itr = (st).begin(); itr != (st).end(); ++itr)
#define irrep(itr, st) for(auto itr = (st).rbegin(); itr != (st).rend(); ++itr)
#define whole(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define bit(n) (1LL<<(n))
// functions
template <typename T> void unique(T& c){c.erase(std::unique(c.begin(), c.end()), c.end());}
template <class T>bool chmax(T &a, const T &b){if(a < b){a = b; return 1;} return 0;}
template <class T>bool chmin(T &a, const T &b){if(a > b){a = b; return 1;} return 0;}
template <typename T> istream &operator>>(istream &is, vector<T> &vec){for(auto &v: vec)is >> v; return is;}
template <typename T> ostream &operator<<(ostream &os, const vector<T>& vec){for(int i = 0; i < vec.size(); i++){ os << vec[i]; if(i + 1 != vec.size())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_set<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T> ostream &operator<<(ostream &os, const unordered_multiset<T>& st){for(auto itr = st.begin(); itr != st.end(); ++itr){ os << *itr; auto titr = itr; if(++titr != st.end())os << " ";} return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p){os << "(" << p.first << ", " << p.second << ")"; return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
template <typename T1, typename T2> ostream &operator<<(ostream &os, const unordered_map<T1, T2> &mp){for(auto itr = mp.begin(); itr != mp.end(); ++itr){ os << "(" << itr->first << ", " << itr->second << ")"; auto titr = itr; if(++titr != mp.end())os << " "; } return os;}
//  types
using ll = long long int;
using P = pair<int, int>;
// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;
const int mod = 1000000007;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};
// io
struct fast_io{
  fast_io(){ios_base::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(20);}
} fast_io_;

int dp[1010][1010];

int main(int argc, char const* argv[])
{
  int q; cin >> q;
  rep(i_, q){
    string s, t; cin >> s >> t;
    rep(i, sz(s) + 1)rep(j, sz(t) + 1)dp[i][j] = 0;
    rep(i, sz(s)){
      rep(j, sz(t)){
        chmax(dp[i+1][j+1], dp[i][j+1]);
        chmax(dp[i+1][j+1], dp[i+1][j]);
        if(s[i] == t[j])chmax(dp[i+1][j+1], dp[i][j] + 1);
      }
    }
    cout << dp[sz(s)][sz(t)] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

int q;
int main() {
    cin >> q;
    for(int i = 0; i < q; ++i) {
        string x, y;
        cin >> x >> y;
        int xlen = x.length(); int ylen = y.length();

        vector< vector<int> > dp(1010, vector<int>(1010, 0));
        for(int i = 0; i < xlen; ++i) {
            for(int j = 0; j < ylen; ++j) {
                if(x.at(i) == y.at(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[xlen][ylen] << endl;
    }
}

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

int lcs_number(string a1, string a2){
	int n1=a1.size()+1, n2=a2.size()+1;	
	vector <vector <int> > A(n1, vector <int> (n2));

	for (int i1=0; i1<n1; i1++)
		for (int i2=0; i2<n2; i2++)
			if (i1==0 || i2==0) A[i1][i2]=0;
			else if (a1[i1-1]==a2[i2-1])
				A[i1][i2]=A[i1-1][i2-1]+1;
			else if (A[i1-1][i2]>A[i1][i2-1])
				A[i1][i2]=A[i1-1][i2];
			else
				A[i1][i2]=A[i1][i2-1];
	return A[n1-1][n2-1];
}

int main(void){
	int n;
	string text1, text2;
	cin>>n;

	for (int i=0; i<n; i++){
		cin>>text1;
		cin>>text2;
		cout<<lcs_number(text1, text2)<<endl;
	}

	return 0;
}

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 i = 0; i <= n; ++i) {
        c[0][i] = 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 <vector>
#include <string>
using namespace std;

int lcs(string x, string y)
{
    int m = x.length();
    int n = y.length();
    int array[m + 1][n + 1];
    for (int i = 0; i <= m; i++)
    {
        array[i][0] = 0;
    }
    for (int i = 0; i <= n; i++)
    {
        array[0][i] = 0;
    }
    int lcsValue = 0;
    for (int i = 1; i <= m; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (x[i - 1] == y[j - 1])
            {
                array[i][j] = array[i - 1][j - 1] + 1;
                lcsValue = max(lcsValue, array[i][j]);
            }
            else if (array[i - 1][j] >= array[i][j - 1])
            {
                array[i][j] = array[i - 1][j];
            }
            else
            {
                array[i][j] = array[i][j - 1];
            }
        }
    }
    return lcsValue;
}

int main()
{
    int num;
    cin >> num;
    for (int i = 0; i < num; i++)
    {
        string x;
        string y;
        cin >> x >> y;
        int ans = lcs(x, y);
        cout << ans << endl;
    }
}