Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = max(dp[i][j-1], dp[i-1][j])
			}
		}
	}
	return dp[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;
#define For(I,A,B) for(int I=(A); I < (B); ++I)
int c[1005][1005];
int main()
{
int t;
string p;
cin>>t;
getline(cin,p);
while(t--){

int x,y,z,k=0,l,i,j;
string a,b;

 For(i,0,1005)
 For(j,0,1005)
 c[i][j]=0;

 getline(cin,a);
 getline(cin,b);
 x=a.length();
 y=b.length();

 For(i,1,x+1)
 For(j,1,y+1)
 {
     if(a[i-1]==b[j-1]) c[i][j]=c[i-1][j-1]+1;
     else  c[i][j]=max(c[i-1][j],c[i][j-1]);
 }
    cout<<c[x][y]<<endl;

}

    return 0;
}

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

int main() {
	int setN;
	cin >> setN;
	string A,B;

	for (int i=0;i<setN;i++) {
		cin >> A;
		cin >> B;
		int L[1000][1000] ={};

		for(int j=1;j<A.length()+1;j++) {
			for(int l=1;l<B.length()+1;l++) {
				if(A[j-1] == B[l-1]) {
					L[j][l] = 1+L[j-1][l-1];
					cout << "a" << endl;
				} else {
					L[j][l] = max(L[j-1][l],L[j][l-1]);
				}
				
			}
		}
		/*
		for(int j=0;j<A.length()+1;j++) {
			cout << endl;
			for(int l=0;l<B.length()+1;l++) {
				cout << L[j][l] << " ";
				
			}
		}
		*/

		cout << L[A.length()][B.length()] << endl;


	}
}

C++: #include<bits/stdc++.h>
using namespace std;
#define int long long
struct node{
  int value;
  node *next;
  node(int value,node *next):value(value),next(next){}
};
int INF=1LL<<55LL;
string lcs(string x,string y){
  int n=x.size(),m=y.size();
  map<char,vector<int> > M;
  for(int j=m-1;j>=0;j--) M[y[j]].push_back(j);
  vector<int> xs(n+1,INF);
  xs[0]=-INF;
  vector<node* > link(n+1);
  for(int i=0;i<n;i++){
    if(M.count(x[i])){
      vector<int> ys=M[x[i]];
      for(int j=0;j<(int)ys.size();j++){
	int k=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),ys[j]));
	xs[k]=ys[j];
	link[k]=new node(y[ys[j]],link[k-1]);
      }
    }
  }
  string res;
  int l=distance(xs.begin(),lower_bound(xs.begin(),xs.end(),INF-1))-1;
  for(node *p=link[l];p!=NULL;p=p->next)
    res.push_back(p->value);
  return res;
}
signed main(){
  int q;
  cin>>q;
  while(q--){
    string x,y;
    cin>>x>>y;
    cout<<lcs(x,y).size()<<endl;
  }
  return 0;
}
/*
verified on 2017/06/22
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C
*/

C++: #include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int dp[1002][1002];

int main(){
	int n;cin >> n;
	string x, y;
	while(n--){
		fill((int*)dp, (int*)(dp+1002), 0);
		cin >> x >> y;
		for(int i = 1;i <= x.size();i++){
			for(int j = 1;j <= y.size();j++){
				if(x[i] == y[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[x.size()][y.size()] << endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)

		int dp[1010][1010];
int main(){
	int n;
	cin>>n;
	lp(z,n){
		string s1,s2;
		cin>>s1>>s2;
		lp(i,s1.size()+1){
			lp(j,s2.size()+1){
				dp[i][j]=0;
			}
		}
		lp(i,s1.size()){
			lp(j,s2.size()){
				dp[i+1][j+1]=max(dp[i+1][j+1],max(dp[i+1][j],dp[i][j+1]));
				if(s1[i]==s2[j]){
					dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
				}
			}
		}
		cout<<dp[s1.size()][s2.size()]<<endl;
	}
	return 0;
}

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

const int MAX = 1010;
int dp[MAX][MAX];

int main()
{
  int n; cin >> n;
  while(n--) {
    string s1, s2; cin >> s1 >> s2;
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < s1.size(); i++)
      for (int j = 0; j < s2.size(); j++)
        if (s1[i] == s2[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[s1.size()][s2.size()] << endl;
  }
  return EXIT_SUCCESS;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <cmath>
#include <set>
#include <algorithm>
#include <iomanip>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL<<60;
const double PI=3.14159265358979323846;
const int NMAX=100005;
using namespace std;


void solve(string x, string y){
    int lx = x.length();
    int ly = y.length();
    int dp[1001][1001];
    for(int i = 0; i < 1001; i++){
        for(int j = 0; j < 1001; j++){
            dp[i][j] = 0;
        }
    }

    for(int i = 1; i <= lx; i++){
        for(int j = 1; j <= ly; j++){
            if(x[i-1] == y[j-1]){
                dp[i][j] = max(dp[i][j-1],dp[i-1][j-1]+1);
                dp[i][j] = max(dp[i][j],dp[i-1][j]);
            } else {
                dp[i][j] = max(dp[i][j-1],dp[i-1][j-1]);
                dp[i][j] = max(dp[i][j],dp[i-1][j]);
            }
        }
    }

    cout << dp[lx][ly] << endl;
}

int main(){
    int q;
    string x,y;
    cin >> q;
    for(int i = 0; i < q; i++){
        cin >> x >> y;
        solve(x,y);
    }

    return 0;

}

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

#if 0
所用時間:0.5h

感想:

#endif

int solve(string X, string Y){
  int rows = Y.size();
  int cols = X.size();
  int L[1001][1001] = {}; //L[y][x]の初期化

  //テーブルLの更新
  for(int r = 1; r <= rows; ++r){
    for(int c = 1; c <= cols; ++c){
      if(X[c] == Y[r]) L[r][c] = 1 + L[r-1][c-1];
      else L[r][c] = max(L[r][c-1], L[r-1][c]);
    }
  }

  // //テーブルの表示
  // cout << "Ltable:" << endl;
  // for(int row=0; row <= rows; ++row){
  //   for(int col = 0; col <= cols; ++col){
  //     cout << L[row][col];
  //   }
  //   cout << endl;
  // }

  return L[rows][cols];
}

int main(){
  int q;
  string X, Y;
  cin >> q;

  for(int i = 0; i < q; ++i){
    cin >> X >> Y;

    int ans = solve(X, Y);

    cout << ans << endl;
  }
}

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

int lcs(string s1, string s2) {
    int n = s1.size(), m = s2.size();

    int c[n + 1][m + 1];
    for (int i = 0; i <= n; i++)
    {
        c[i][0] = 0;
    }
    for (int i = 0; i <= m; i++) {
        c[0][i] = 0;
    }

    // indexを合わせるため、各文字列に空白を挿入
    s1 = ' ' + s1;
    s2 = ' ' + s2;

    int maxl=0;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= m; j++) {
            if (s1.at(i) == s2.at(j))
                c[i][j] = 1 + c[i - 1][j - 1];
            else
            {
                c[i][j] = max(c[i - 1][j], c[i][j - 1]);
            }
            maxl = max(maxl, c[i][j]);
        }
    }
    return maxl;
}

int main() {
    int q;
    cin >> q;

    for (int i = 0; i < q; i++)
    {
        string s1, s2;
        cin >> s1 >> s2;
        cout << lcs(s1, s2) << endl;
    }
}

C++: def lcs(str_a,str_b):    
    if len(str_a) == 0 or len(str_b) == 0:
        return 0
    dp = [[0 for kb in range(len(str_b) + 1)] for ka in range(len(str_a) + 1)]
    for i in range(1, len(str_a) + 1):
        for j in range(1, len(str_b) + 1):
            if str_a[i-1] == str_b[j-1]:
                dp[i][j] = dp[i-1][j-1] + 1
            else:
                dp[i][j] = max([dp[i-1][j], dp[i][j-1]])
  #print ("length of LCS is :",dp[len(str_a)][len(str_b)])
    return dp[len(str_a)][len(str_b)]

n=int(input())
for i in range(n):
    s1=input()
    s2=input()
    print(lcs(s1,s2))