Go: package main

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

// define
var (
	sc = bufio.NewScanner(os.Stdin)
)

func bordersInt(in_map [][]int, invalid int) [][]int{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]int, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]int, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func bordersString(in_map [][]string, invalid string) [][]string{
	H, W := len(in_map), len(in_map[0])
	out_map := make([][]string, H+2)
	for h:=0; h<H+2; h++ {
		out_map[h] = make([]string, W+2)
		for w:=0; w<W+2; w++ {
			out_map[h][w] = invalid
		}
	}
	for h:=0; h<H; h++{
		for w:=0; w<W; w++{
			out_map[h+1][w+1] = in_map[h][w] 
		}
	}
	return out_map
}

func scanIntLine(len int) []int{
	arr := make([]int, len)
	for i:=0; i<len; i++{
		sc.Scan()
		tmp, err := strconv.Atoi(sc.Text())
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func scanStringLine(len int) []string{
	arr := make([]string, len)
	for i:=0; i<len; i++{
		sc.Scan()
		arr[i] = sc.Text()
	}
	return arr
}

func scanIntSplit(len int) []int{
	arr := make([]int, len)
	sc.Scan()
	for i, v := range strings.Split(sc.Text(), " "){
		tmp, err := strconv.Atoi(v)
		if err == nil{
			arr[i] = tmp
		}
	}
	return arr
}

func maxInt(args... int) int{
	sort.Ints(args)
	return args[len(args)-1]
}

func main(){
	buf := make([]byte, 0)
    sc.Buffer(buf, 1000000009)
	var n int 
	fmt.Scan(&n)
	dp := make([][]int, 1003)
	for i:=0; i<1003; i++ {
		dp[i] = make([]int, 1003)
	}
	input_lines := scanStringLine(2*n)
	for i:=0; i<n; i++ {
		S, T := input_lines[2*i], input_lines[2*i+1]
		lenS, lenT := len(S), len(T)
		for i:=1; i<lenS+1; i++ {
			for j:=1; j<lenT+1; j++ {
				if S[i-1] == T[j-1] {
					dp[i][j] = maxInt(dp[i-1][j-1]+1, dp[i-1][j], dp[i][j-1])
				} else {
					dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
				}
			}
		}
		fmt.Println(dp[lenS][lenT])
	}
}

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

const int MAX_X = 1000, MAX_Y = 1000;

int main() {
  int n;
  int dp[MAX_X+1][MAX_Y+1];
  string X, Y;
  cin >> n;
  for (int k=0; k<n; k++) {
    cin >> X >> Y;
    for (int i=0; i<=X.length(); i++) {
      for (int j=0; j<=Y.length(); j++) {
        dp[i][j] = 0;
      }
    }

    for (int i=1; i<=X.length(); i++) {
      for (int j=1; j<=Y.length(); j++) {
        if (X[i-1] == Y[j-1]) {
          dp[i][j] = dp[i-1][j-1] + 1;
        } else {
          dp[i][j] = max(dp[i][j-1], dp[i-1][j]);
        }
      }
    }

    cout << dp[X.length()][Y.length()] << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

string X, Y;
int lc[1005][1005];

int getLCS(int m, int n) {
	if (m < 0 || n < 0)
		return 0;
	if (lc[m][n] != -1)
		return lc[m][n];
	if (X[m] == Y[n]) 
		return lc[m][n] = getLCS(m - 1, n - 1) + 1;
	else
		return lc[m][n] = max(getLCS(m, n - 1), getLCS(m - 1, n));
}

int main(void) {
	int i, j;
	int nu;
	cin >> nu;
	while (nu--) {
		for (i = 0; i < 1005; i++) 
			for (j = 0; j < 1005; j++) 
				lc[i][j] = -1;
		cin >> X >> Y;
		int m = X.length() - 1, n = Y.length() - 1;
		int lcs = getLCS(m, n);
		cout << lcs << endl;
	}
}

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

typedef long long llong;

using namespace std;

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);

	int q;
	cin >> q;
	while (q--) {
		string a, b;
		cin >> a >> b;
		int n = a.length(), m = b.length();
		vector<int> prev(m + 1, 0);
		for (int i = 1; i <= n; ++i) {
			vector<int> current(m + 1);
			current[0] = 0;
			for (int j = 1; j <= m; ++j) {
				if (a[i - 1] == b[j - 1]) {
					current[j] = 1 + prev[j - 1];
				} else {
					current[j] = max(current[j - 1], prev[j]);
				}
			}
			prev = current;
		}
		cout << prev[m] << '\n';
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

// ---------------------
// repetition
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

// debug
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

// ---------------------
#define INF 922337203685477580
typedef long long ll;

int dp[1000][1000];

int main() {
  int q;
  cin >> q;
  REP(loop, q) {
    char S[1000], T[1000];
    cin >> S >> T;

    dp[0][0] = 0;
    for (int i = 1; i <= strlen(S); i++) {
      for (int j = 1; j <= strlen(T); j++) {
        if (S[i] == T[j]) {
          dp[i][j] = dp[i - 1][j - 1] + 1;
        } else {
          dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
        }
      }
    }
    cout << dp[strlen(S)][strlen(T)] << endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <string.h>

#define N 10000

int lcs(char *x,char *y){
	int c[N+1][N+1];
	int m = strlen(x);
	int n = strlen(y);
	int maxl = 0;
	x = strcat(" ",x);
	y = strcat(" ",y);
	int i,j;
	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]==y[j]){
				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];
			}
			if(maxl>c[i][j])maxl = maxl;
			else maxl = c[i][j];
		}
	}
	return maxl;
}

int main(){
	char s1[1000];
	char s2[1000];
	int n;
	scanf("%d",&n);
	int i;
	for(i=0;i<n;i++){
		scanf("%s%s",s1,s2);
		printf("%d\n",lcs(s1,s2));
	}
	return 0;
}

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

using namespace std;

int main(){
  int q;cin >>q;
  for(int i=0;i<q;i++){
    string x,y;cin >> x >> y;
    int lx=x.length();int ly=y.length();
    //cout << lx << ly;
    vector< vector<int> > dp(ly,vector<int>(lx,0));
    int f=0;
    for(int j=0;j<ly;j++){
      if(x[0]==y[j]){f=1;}
      dp[j][0]=f;
    }
    f=0;
    for(int j=0;j<lx;j++){
      if(y[0]==x[j]){f=1;}
      dp[0][j]=f;
    }
    for(int j=0;j<ly;j++){
      for(int k=0;k<lx;k++){
        if(j!=0 and k!=0){
          int l=0;
          if(x[k]==y[j]){l=1;}
          //{}でmaxと同じように使える
          dp[j][k]=max({dp[j-1][k],dp[j][k-1],dp[j-1][k-1]+l});
        }
      }
    }
    cout << dp[ly-1][lx-1] << endl;
  }
}

C++: #define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
using namespace std;
#define len(n) (int)n.length()
#define pb push_back
#define ll long long
#define make_pair mkp
#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define ALL(v) (v).begin(), (v).end()
#ifdef DBGPRT
#define dbg(n) cerr << "//" << #n << ":" << n <<endl
#else
#define dbg(n) ;
#endif
typedef pair<ll, ll> LLARR;
#define INF 999999999
const double PI = 3.1415926535897932384626433832795;

int lcs(string s1, string s2)
{
	int m = len(s1);
	int n = len(s2);
	s1 = ' '+s1;
	s2 = ' '+s2;
	int (*mat)[1001];
	mat = new int[1001][1001];
	REP(i, m)
	{
		mat[i][0] = 0;
	}
	REP(i, n)
	{
		mat[0][i] = 0;
	}
	int maxx = 0;
	FOR(i, 1, m+1)
	{
		FOR(j, 1, n+1)
		{
			if (s1[i] == s2[j])
				mat[i][j] = mat[i-1][j-1] + 1;
			else
				mat[i][j] = max(mat[i][j-1], mat[i-1][j]);
			maxx = max(maxx, mat[i][j]);
		}
	}
	delete[] mat;
	return maxx;
}
int main()
{
	ios::sync_with_stdio(false);

	int n;
	cin >> n;
	REP(i, n)
	{
		string s1;
		cin >> s1;
		string s2;
		cin >> s2;
		cout << lcs(s1, s2) << endl;
	}
	return 0;
}