Go: package main

import (
	"bufio"
	"fmt"
	"io"
	"os"
	"strconv"
)

const (
	initialBufSize = 100000
	maxBufSize     = 1000000
)

var sc *bufio.Scanner

func initScanner(r io.Reader) *bufio.Scanner {
	buf := make([]byte, initialBufSize)

	sc := bufio.NewScanner(r)
	sc.Buffer(buf, maxBufSize)
	sc.Split(bufio.ScanWords) // bufio.ScanLines
	return sc
}

func main() {
	sc = initScanner(os.Stdin)
	resolve()
}

func resolve() {
	n := scanInt(sc)
	results := []int{}
	for i := 0; i < n; i++ {
		results = append(results, resolveInstance())
	}
	for _, v := range results {
		fmt.Println(v)
	}
	return
}

func resolveInstance() int {
	x, y := scanString(sc), scanString(sc)
	lx, ly := len(x), len(y)
	dp := make([][]int, lx+1)
	dp[0] = make([]int, ly+1)
	for i := 1; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
		dp[i][0] = dp[i-1][0]
		for j := 1; j <= ly; j++ {
			dp[i][j] = dp[i-1][j]
			if dp[i][j] < dp[i][j-1] {
				dp[i][j] = dp[i][j-1]
			}
			if x[i-1] == y[j-1] {
				cL := dp[i-1][j-1] + 1
				if dp[i][j] < cL {
					dp[i][j] = cL
				}
			}
		}
	}
	return dp[lx][ly]
}

const (
	remove = 0 + iota
	match
)

// snip-scan-funcs
func scanInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return int(i)
}
func scanString(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

C++: #include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <cmath>
#include <ctype.h>
#include <ctime>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <utility>
#include <numeric>
#include <complex>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <cassert>
#include <iostream>
#include <iterator>
#include <algorithm>

using namespace std;

int lcs(string X,string Y){
	int m = X.size();
	int n = Y.size();
	int maxi = 0; 
	int C[m+1][n+1];
	X = ' ' + X;
	Y = ' ' + Y;
	for(int i = 1;i <= m;i++) C[i][0] = 0;
	for(int i = 1;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]);
			}
			maxi = max(maxi,C[i][j]);
		}
	}
	return maxi;
}

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

C++: #include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
//#include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;  cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i,x) for(int i=0;i<x;i++)
#define rep1(i,x) for(int i=1;i<=x;i++)
#define rrep(i,x) for(int i=x-1;i>=0;i--)
#define rrep1(i,x) for(int i=x;i>0;i--)

int q;
string s1,s2;
int ans;
int dp[1510][1501];
int main()
{
	cin>>q;
	rep(i,q){
		cin>>s1>>s2;
		rep(i,s1.size()){
			rep(j,s2.size()){
				dp[i][j]=0;
			}
		}
		if(s1[0]==s2[0]) dp[0][0]=1;
		rep1(i,s1.size()-1){
			if(s1[i]==s2[0]) dp[i][0]=1;
			dp[i][0]=max(dp[i][0],dp[i-1][0]);
		}
		rep1(i,s2.size()-1){
			if(s1[0]==s2[i]) dp[0][i]=1;
			dp[0][i]=max(dp[0][i],dp[0][i-1]);
		}
		rep1(i,s1.size()-1){
			rep1(j,s2.size()-1){
				dp[i][j]=max( dp[i-1][j], dp[i][j-1] );
				if(s1[i]==s2[j]) dp[i][j]= max(dp[i][j],dp[i-1][j-1]+1);
			}
		}
		cout<<dp[s1.size()-1][s2.size()-1]<<endl;
	}
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;

typedef pair<int,int> pii;

vector<pii> P;

string s,t,rev;

const int MAX_N = 1001;

int dp[MAX_N][MAX_N];

void trace(int i,int j){
	if(dp[i][j] == dp[i-1][j] && dp[i][j] == dp[i][j-1]){
		trace(i-1,j-1);	//??????????§????
	}else if(dp[i][j] == dp[i-1][j]){
		trace(i-1,j);	//???????§????
	}else if(dp[i][j] == dp[i][j-1]){
		trace(i,j-1);	//???????§????
	}else{
		rev += s[i-1];
		if(dp[i-1][j-1] == 0){
			return;
		}
		trace(i-1,j-1);	//??????????§????
	}
}
int main()
{
	string com;
	int n;
	cin >> n;
	for(int l=0;l<n;l++){
	cin >> s;
	cin >> t;
	for(int i=0;i<=s.length();i++){
		dp[i][0] = 0;
	}
	for(int i=0;i<=t.length();i++){
		dp[0][i] = 0;
	}
	for(int i=1;i<=s.length();i++){
		for(int j=1;j<=t.length();j++){
			if(s[i] == t[j]){
				dp[i][j] = dp[i-1][j-1]+1;
			}else{
				dp[i][j] = max(dp[i][j-1],dp[i-1][j]);
			}
		}
	}
	printf("%d\n",dp[s.length()][t.length()]);
	fill(dp[0],dp[s.length()+1],0);
	}
	/*
	trace(s.length(),t.length());
	for(int i=rev.length();i>0;i--){
		com += rev[i-1];
	}
	cout << com << "\n";	//????????±?????¨???????????????????????????
	*/
}

C++: #include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>

using namespace std;
static const int N=1000;
int lcs(string X, string Y){
    int m=X.size();
    int n=Y.size();
    int c[N+1][N+1];
    int maxl=0;
    X=' '+X;
    Y=' '+Y;
    int i,j;
    for(i=1;i<=m;i++)c[i][0]=0;
    for(j=1;j<=n;j++)c[0][j]=0;

    for(i=1;i<=m;i++){
        for(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][j-1],c[i-1][j]);
            }
            maxl=max(maxl,c[i][j]);
        }
    }
    return maxl;
}
int main()
{
    int n;
    string s1,s2;
    cin>>n;
    for(int i=0;i<n-1;i++){
        cin>>s1>>s2;
        cout<<lcs(s1,s2)<<endl;
    }
    cin>>s1>>s2;
    cout<<lcs(s1,s2);
    return 0;
}

C++: #include <iostream>
#include <cstring>

using namespace std;

/**
 *  lcs(m,n) = 
 *  if x[m] == y[n] lcs[m-1,n-1]+1
 *  max(lcs[m-1,n],lcs[m,n-1])
 *
 */

string x, y;

const int N = 1000;

int c[N][N];

int lcs(int m, int n)
{
	if (m == -1 || n== -1) return 0;
	if (x[m] == y[n]) return lcs(m-1,n-1) + 1;
	int & cc = c[m][n];
	if (cc != -1) return cc;
	int xx = lcs(m-1,n);
	int yy = lcs(m,n-1);
	cc = xx > yy ? xx : yy;
	return cc;
}

int main()
{
	int n = 0;
	x.reserve(N);
	y.reserve(N);
	cin >> n; 
	for (int i=0;i<n;i++)
	{
		memset(c, -1, sizeof(c));
		cin >> x;
		cin >> y;
		cout << lcs(x.size()-1, y.size()-1) << endl;
		x.clear();
		y.clear();
	}
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>

int find_lcs(std::string s1, std::string s2)
{
  // DP table
  int s1_len = s1.size();
  int s2_len = s2.size();

  int dp[s1_len+1][s2_len+1];
  std::memset(dp, 0, sizeof(dp));

  for ( int i = 0; i < s1_len; i++ ) {
    for ( int j = 0; j < s2_len; j++ ) {
      if ( s1[i] == s2[j] ) {
        dp[i+1][j+1] = dp[i][j] + 1;
      } else {
        dp[i+1][j+1] = std::max(dp[i][j+1], dp[i+1][j]);
      }
    }
  }

  return dp[s1_len][s2_len];
}

int main(void)
{
  int q;
  std::cin >> q;
  for ( int i = 0; i < q; i++ ) {
    std::string s1, s2;
    std::cin >> s1 >> s2;
    std::cout << find_lcs(s1, s2) << std::endl;
  }
  return 0;
}