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++: #include <iostream>
#include <algorithm> // next_permutation
#include <iomanip>
#include <cmath>
#include <vector>
#include <sstream>
#include <string>
#include <cstdio>
#include <stack>
#include <queue>
#include <list>
#include <numeric> //accumulate
//#include <unordered_map> //???????????\??¢??°
#include <fstream> //ifstream, ofstream

//#define NDEBUG //NDEBUG???#include <cassert>???????????????????????????????????´???assert?????????????????????????????????NDEBUG?????????????????????????????????????????????
#include <cassert> //assert


using namespace std;

//???????????°??????TEST????????????????????¬???????????????????????????????????¢????????????
#define TEST //*******************************************************************************************************************************************
//?????????????????¨??§?????\????????????????????????????????°?????????????¨???????????????????dout???????????????????????§???????????¬??????????????????????????????????????????
//??????????????????????????????????????????????????????????§?CPU???????£??????????????????§???TLE?????????????????????????????????????????§????????¨???                           ????????????????????????cerr????????£????????????????????????????????????
#ifdef TEST
#define dout cout
#define din cin
#else
stringstream dummy; //???????????°??????dout?????????????????????????????????
#define dout dummy.str(""); dummy.clear(stringstream::goodbit); dummy //dummy?????????????????????????????????????????¨?????¢?????¢???????????????
//???????????¨??????????????????goodbit?????????????????????????????¨???????????´????????????????????????????????¨?????°?????????????????§???????????????????????????????????? http://d.hatena.ne.jp/linden/20060427/p1
#endif

//?¨??????????????????????????????????????????????????????´??????OUTPUTFILE????????????????????¬???????????????????????????????????¢????????????
//#define OUTPUTFILE "output.txt" //*******************************************************************************************************************************************
#ifdef OUTPUTFILE
#define dout outputfile //??¨????????????TLE????????§?????????dout?????¨??¨??????????????¢????????????????????????????????????????????§????¨????????????´??????????????£??????????????????????¨?????????????????????????????????????????????§?????????????????????
ofstream outputfile(OUTPUTFILE);
#define OutputFilePath "/Users/Nag/Documents/Prgm/Test/DerivedData/Test/Build/Products/Debug/output.txt"
#endif

//?¨??????\???????????????????????????????????????????????´??????INPUTFROMTEXTFILE????????????????????¬???????????????????????????????????¢????????????
//#define INPUTFILE "input.txt" //*******************************************************************************************************************************************
#ifdef INPUTFILE
#define din inputfile
ifstream inputfile(INPUTFILE);
#endif


#define disp(A) dout << #A << " = " << (A) << endl
#define disP(A) dout << setw(3) << (A) << " " // << setw(3) ??????????????\????????????
#define rep(i,a,n) for(int (i)=(a); (i)<(n); (i)++)
#define dispAll(A,n) dout << #A << " = "; rep(j, 0, (n)) {disP(A[j]);} dout << endl

typedef pair<int,int> pii;
typedef vector<int> vi;
typedef unsigned long long ll;


const int INF = 999;//1e9-1;

int dp[1001][1001] = {0};
pii from[1001][1001];

string X, Y;



int solve(int i, int j) {
//    disp(i); disp(j);
    
    int ans;
    
    if(i<0 or j<0) return 0;
    
    if(dp[i][j]!=0) return dp[i][j];
    
    if(X[i]==Y[j]) {
        ans = solve(i-1,j-1) + 1;
        from[i][j] = make_pair(i-1, j-1);
    } else {
        if( solve(i-1,j) > solve(i,j-1) ) {
            ans = solve(i-1,j);
            from[i][j] = make_pair(i-1, j);
        } else {
            ans = solve(i,j-1);
            from[i][j] = make_pair(i, j-1);
        }
    }
    
    dp[i][j] = ans;

//    //test display
//    dout<<"-------------------------------------\n";
//    dout<<"                ";
//    rep(j,0,Y.size()) disP(j); dout<<endl;
//    dout<<"                ";
//    rep(j,0,Y.size()) disP(Y[j]); dout<<endl;
//    rep(i,0,X.size()) {
//        disP(i); disP(X[i]); dispAll(dp[i], Y.size());
//    }
    
    return ans;
}


int main() {
    int q; din >> q;
    rep(i,0,q) {
        din >> X >> Y;
//        disp(X); disp(Y);
//        dout<<solve(X.size()-1,Y.size()-1)<<endl;
//        
//        rep(i,0,X.size()) {
//            rep(j,0,Y.size()) {
//                dp[i][j]=0;
//                from[i][j] = make_pair(-1, -1);
//            }
//        }
        
        rep(i,0,X.size()+1) {
            rep(j,0,Y.size()+1) {
                dp[i][j]=0;
            }
        }
        
        rep(i,1,X.size()+1) {
            rep(j,1,Y.size()+1) {
                if(X[i-1]==Y[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                }
                else {
                    dp[i][j] = max( dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        
//            //test display
//            dout<<"-------------------------------------\n";
//            dout<<"                ";
//            rep(j,0,Y.size()+1) disP(j); dout<<endl;
//            dout<<"                    ";
//            rep(j,1,Y.size()+1) disP(Y[j-1]); dout<<endl;
//            rep(i,0,X.size()+1) {
//                if(i==0) {
//                    disP(i); disP(0); dispAll(dp[i], Y.size()+1);
//                } else {
//                    disP(i); disP(X[i-1]); dispAll(dp[i], Y.size()+1);
//                }
//            }
        
        dout<<dp[X.size()][Y.size()]<<endl;
        
    }
    
    
    
#ifdef INPUTFILE
    inputfile.close();
#endif
    
#ifdef OUTPUTFILE
    outputfile.close();
    cout << "\"" << OutputFilePath << "\"" << endl;
#endif
    
    return 0;
}

C++: #include<iostream>
#include<string>
int main()
{
  int n;
  int an[1000];
  int l=0;
  std::cin>>n;
  std::string x;
  std::string y;
  int ans[1000][1000];
  int j,k;
  for(int i=0;i<n;i++){
    std::cin>>x;
    std::cin>>y;
    for(int j=0;j<1000;j++){
      for(int k=0;k<1000;k++){
	ans[j][k]=0;
      }
    }
    for(int j=1;j<x.size();j++){
      for(int k=1;k<y.size();k++){
	if(x[j]==y[k]){
	  ans[j][k]=ans[j-1][k-1]+1;
	}else{
	  if(ans[j][k-1]>ans[j-1][k]){
	    ans[j][k]=ans[j][k-1];
	  }else{
	    ans[j][k]=ans[j-1][k];
	  }
	}
      }
    }
    std::cout<<ans[x.size()-1][y.size()-1]<<'\n';  
  }
  return 0;
}