Go: package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {
	for q := ReadInt(); q > 0; q-- {
		x, y := ReadString(), ReadString()
		fmt.Println(LCS(x, y))
	}
}

func LCS(x, y string) int {
	dp := make([][]int, len(x)+1)
	for i := 0; i < len(x)+1; i++ {
		dp[i] = make([]int, len(y)+1)
	}
	for i := 0; i < len(x); i++ {
		for j := 0; j < len(y); j++ {
			dp[i+1][j+1] = Max(dp[i+1][j], dp[i][j+1])
			if x[i] == y[j] {
				dp[i+1][j+1] = Max(dp[i+1][j+1], dp[i][j]+1)
			}
		}
	}
	return dp[len(x)][len(y)]
}

func Max(xs ...int) int {
	max := xs[0]
	for _, x := range xs[1:] {
		if max < x {
			max = x
		}
	}
	return max
}

var reader = bufio.NewReader(os.Stdin)

func Scan(a ...interface{}) {
	if _, err := fmt.Fscan(reader, a...); err != nil {
		panic(err)
	}
}
func ReadInt() (i int)       { Scan(&i); return }
func ReadString() (s string) { Scan(&s); return }
func ReadInts(n int) []int {
	v := make([]int, n)
	for i := 0; i < n; i++ {
		Scan(&v[i])
	}
	return v
}

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 dp[i][j] = 0;
    
    if(dp[i][j]!=0) return dp[i][j];
    
    if(X[i-1]==Y[j-1]) {
        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(),Y.size())<<endl;

        rep(i,0,X.size()) {
            rep(j,0,Y.size()) {
                dp[i][j]=0;
//                from[i][j] = make_pair(-1, -1);
            }
        }
        
//        //initialize
//        rep(i,0,X.size()+1) {
//            rep(j,0,Y.size()+1) {
//                dp[i][j] = 0;
//                from[i][j] = make_pair(-1, -1);
//            }
//        }
//        
//        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<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>


using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };

#define ARRAY_MAX 1005

const int INF = 1e9 + 7;
const int MOD = 1e9 + 7;

int dp[ARRAY_MAX][ARRAY_MAX];


int main() {

	int n;
	cin >> n;
	for (int tm = 0; tm < n; tm++)
	{
		string s, t;
		cin >> s >> t;
		int s_siz = s.size();
		int t_siz = t.size();

		for (int i = 0; i < s_siz; i++) {
			for (int j = 0; j < t_siz; 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_siz][t_siz] << endl;

	}

	return 0;
}