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<stdio.h>
#include<string.h>
 
char X[1001],Y[1001];
 
int LCS_Length(void){
  int i,j;
  int m = strlen(X);
  int n = strlen(Y);
  int c[m+1][n+1];
 
  for(i=1 ; i<=m ; i++) c[i][0] = 0;
  for(j=0 ; j<=n ; j++) c[0][j] = 0;
   
  for(i=1 ; i<=m ; i++){
    for(j=1 ; j<=n ; j++){
      if(X[i-1] == Y[j-1]) c[i][j] = c[i-1][j-1] + 1;
      else if(c[i-1][j] >= c[i][j-1]) c[i][j] = c[i-1][j];
      else c[i][j] = c[i][j-1];
    }
  }
  return c[m][n]; 
}
 
main(){
  int i,n;
  scanf("%d",&n);
  for(i=0 ; i<n ; i++){
    scanf("%s%s",X,Y);
    printf("%d\n",LCS_Length());
  }
  return 0;
}

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

using namespace std;
static const int N = 100;
static const int INFTY = (1<<21);

int n, M[N][N];
int d[N];

void bfs(int s) {
    queue<int> q;
    q.push(s);
    for(int i = 0; i < n; i++) d[i] = INFTY;
    d[s] = 0;
    int u;
    while( !q.empty() ) {
        u = q.front(); q.pop();
        for (int v = 0; v < n; v++) {
            if( M[u][v] == 0 || d[v]!= INFTY ) continue;
            d[v] = d[u] + 1;
            q.push(v);
        }
    }
    for (int i = 0; i < n; i++) {
        cout << i+1 << " " << (d[i] == INFTY ? (-1) : d[i]) << endl;
    }
}

int main() {
    int u, k, v;

    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            M[i][j] = 0;
        }
    }

    for(int i = 0; i < n; i++) {
        cin >> u >> k;
        u--;
        for (int j = 0; j < k; j++) {
            cin >> v;
            v--;
            M[u][v] = 1;
        }
    }

    bfs(0);

    return 0;
}

C++: #include<stdio.h>
#include<string.h>
    
int LCS(char *X, char *Y){
  int i,j,m,n,c[1001][1001];
  m = strlen(X);
  n = strlen(Y);
      
  for(i=0; i<m; i++){
    c[i][0] = 0;
  }
  for(j=0; j<n; j++){
    c[0][j] = 0;
  }
    
  for(i=1;i<m+1;i++){
    for(j=1;j<n+1;j++){
      if (X[i-1]==Y[j-1]) c[i][j]=c[i-1][j-1]+1;
      else if (c[i-1][j]>=c[i][j-1]) c[i][j]=c[i-1][j];
      else c[i][j]=c[i][j-1];
    }
  }
  return c[i-1][j-1];
}
    
int main(){
  int i, q;
  char X[1001], Y[1001];
      
  scanf("%d", &q);
  for(i=0; i<q; i++){
    scanf("%s", X);
    scanf("%s", Y);
    printf("%d\n", LCS(X, Y));
    
  }
  return 0;
}

C++: #include<iostream>
#include<string>
#include<algorithm>
#define MAX 1000
using namespace std;

int LCS(string X, string Y) {
    int i, j, maxl=0;
    int C[MAX+1][MAX+1];

    int X_len = X.size();
    int Y_len = Y.size();
    X = ' ' + X;
    Y = ' ' + Y;
    for (i=0; i<=X_len; i++) C[i][0]=0;
    for (j=1; j<=Y_len; j++) C[0][j]=0;

    for (i=1; i<=X_len; i++) {
        for (j=1; j<=Y_len; j++) {
            if (X[i]==Y[j]) {
                C[i][j]=C[i-1][j-1]+1;
                maxl = max(C[i][j], maxl);
            } else C[i][j]=max(C[i-1][j], C[i][j]);
        }
    }

    return maxl;
}

int main() {
    int n, i;
    string X, Y;
    
    cin >> n;
    for (i=0; i<n; i++) {
        cin >> X >> Y;
        cout << LCS(X, Y) << endl;
    }

    return 0;
}

C++: #include <iostream>
#include <cstring>
#include <algorithm>
//#define Max 2000000
using namespace std;
int dp[1001][1001];
char x[1000],y[1000];
void init(){
    for(int i=0;i<1000;i++)
        for(int j=0;j<1000;j++)
            dp[i][j]=0;
}
void LCS(){
    int m=strlen(x);
    int r=strlen(y);
    for(int i=1;i<=m;i++)
        for(int j=1;j<=r;j++)
        {
            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]);
            }
        }
}
int main()
{
    int q;
    cin>>q;
    while((q--)>0){
        cin>>x;
        cin>>y;
        init();
        LCS();
        cout<<dp[strlen(x)][strlen(y)]<<endl;
    }
}