Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <iostream>
#include <string>
using namespace std;
static const int MAX = 1001;
 
void lcs(string X, string Y) {
    int dp[MAX][MAX];  
    int i,j;
 
    int xLength = X.size();
    int yLength = Y.size();
 
 
    // initialize dp[][]
    for (i = 1; i <= xLength; i++)
        dp[i][0] = 0;
    for (i = 1; i <= yLength; i++)
        dp[0][i] = 0;
 
    for (i = 1; i <= xLength; i++){
        for (j = 1; j <= yLength; j++){
            // Because the original string is 0-based , so subtracted from 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]);
            }
        }
    }
 
    // testing code: print out the dp[][]
    // cout << "j";
    // for (j = 1; j <= yLength; j++)
    //  cout << ' ' << j ;
    // cout << endl;
         
 
    // for (i = 1; i <= xLength; i++){
    //  cout << i << '.';
    //  for (j = 1; j <= yLength; j++){
    //      cout << dp[i][j] << ' ';
    //  }
    //  cout << endl;
    // }
 
    cout << dp[xLength][yLength] << endl;
}
 
int main() {
    int n , i;
    string X, Y;
 
    cin >> n;
    for (i = 0 ; i < n; i++ ) {
        cin >> X;
        cin >> Y;
        lcs(X, Y);
    }
 
    return 0;
 
}

C++: #include<iostream>
#include<string>
#include<algoritm>
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 = 1;i<=m;i++) c[i][0] = 0;
  for(int j = 1; j<=n;j++) c[0][j] = 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++: //
#if 0

#endif // 0
#include<iostream>
#define MAX 1010
using namespace std;
int l[MAX][MAX];

int main()
{
    int num;
    cin>>num;       // int l[MAX][MAX];

    for(int i=0;i<num;i++)
    {
        string s1,s2;
        cin>>s1>>s2;
        //int l[MAX][MAX];
        for(int m=0;m<=s1.size();m++)
            for(int n=0;n<=s2.size();n++)
            {
                if(m<=0||n<=0)
                    l[m][n]=0;
                else if(s1[m-1]==s2[n-1])
                    l[m][n]=l[m-1][n-1]+1;
                else
                    l[m][n]=max(l[m][n-1],l[m-1][n]);
            }
        cout<<l[s1.size()][s2.size()]<<endl;
    }
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
using namespace std;
#define INF (1 << 30) - 1
#define LINF 1LL << 60
const int MOD = 1000000007;
using ll = long long;
using P = pair<int, int>;

int dp[1010][1010];

int main(){
  int q;
  cin >> q;
  for(int i = 0; i < q; ++i){
    string x, y;
    cin >> x >> y;
    fill((int*)dp, (int*)dp + sizeof(dp) / sizeof(int), 0);
    // fill((int*)dp, (int*)(dp + 1010), 0);

    int xs = x.size();
    int ys = y.size();

    for(int i = 0; i < xs; ++i){
      for(int j = 0; j < ys; ++j){
        if(x[i] == y[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[xs][ys] << endl;
  }

  return 0;
}

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

std::string x,y;
int lcs[1000][1000] = {0};

int max(int k,int l){
  if(k>l){
    return k;
  }else{
    return l;
  }
}

void LCS(int i,int j){
  if(x[i] == y[j]){
    lcs[i][j] = lcs[i-1][j-1]+1;
  }else{
    lcs[i][j] = max(lcs[i-1][j],lcs[i][j-1]);
  }
}

void pre(){
  for(int i=0;i<(int)y.size();i++){
    if(x[0] == y[i]){
      lcs[0][i] = 1;
    }else{
      lcs[0][i] = 0;
    }
  }
  for(int i=0;i<(int)x.size();i++){
    if(x[i] == y[0]){
      lcs[i][0] = 1;
    }else{
      lcs[i][0] = 0;
    }
  }
}

int main(){
  int q;
  std::cin >> q;
  for(int k=0;k<q;k++){
    std::cin >> x;
    std::cin >> y;
    int size_x=x.size(),size_y=y.size();
    pre();
    for(int i=1;i<size_x;i++){
      for(int j=1;j<size_y;j++){
	LCS(i,j);
      }
    }

    /*for(int i=0;i<size_x;i++){
      for(int j=0;j<size_y;j++){
	std::cout << lcs[i][j] << ' ';
      }
      std::cout << '\n';
    }
    */
    std::cout << lcs[size_x-1][size_y-1] << '\n'; 

  }
  return 0;
}

C++: #include<iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(int i=0;i<n;i++)

/* 大文字を小文字に変換 */
char tolower(char c) {return (c + 0x20);}
/* 小文字を大文字に変換 */
char toupr(char c) {return (c - 0x20);}

// if('A'<=s[i] && s[i]<='Z') s[i] += 'a'-'A';

/*
string s = "abcdefg"
s.substr(4) "efg"
s.substr(0,3) "abc"
s.substr(2,4) "cdef"
*/

int LCS(string s1, string s2){
    int c[s1.size()+1][s2.size()+1];
    rep(i,s1.size()+1) rep(j, s2.size()+1) c[i][j] = 0;
    for(int i=1; i<=s1.size(); i++){
        for(int j=1; j<=s2.size(); j++){
            if(s1[i-1] == s2[j-1]){
                c[i][j] = c[i-1][j-1] + 1;
            }else{
                c[i][j] = max(c[i-1][j], c[i][j-1]);
            }
        }
    }
    return c[s1.size()][s2.size()];
}

int main()
{
    int q; cin >> q;
    rep(i,q){
        string s1, s2;
        cin >> s1 >> s2;
        cout << LCS(s1, s2) << endl;
    }
}