Go: package main

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

//Util
func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

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

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

func absInt(a int) int {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func absFloat64(a float64) float64 {
	if a >= 0 {
		return a
	} else {
		return -a
	}
}

func minFloat64(a, b float64) float64 {
	if a > b {
		return b
	} else {
		return a
	}
}

func str2Int(s string) int {
	n, _ := strconv.Atoi(s)
	return n
}

//Main
var sc = bufio.NewScanner(os.Stdin)
var q int
var X, Y string

func lcs(X, Y string) int {
	lenX := len(X)
	lenY := len(Y)
	dp := make([][]int, lenX+1)
	for i := 0; i < lenX+1; i++ {
		dp[i] = make([]int, lenY+1)
	}

	res := 0
	for i := 1; i < lenX+1; i++ {
		for j := 1; j < lenY+1; j++ {
			if X[i-1] == Y[j-1] {
				dp[i][j] = dp[i-1][j-1] + 1
			} else {
				dp[i][j] = maxInt(dp[i-1][j], dp[i][j-1])
			}
			//fmt.Println(dp)
			res = maxInt(res, dp[i][j])
		}

	}
	return res
}

func main() {
	sc.Split(bufio.ScanWords)
	q = nextInt()

	for i := 0; i < q; i++ {
		X = nextStr()
		Y = nextStr()

		fmt.Println(lcs(X, Y))
	}
}

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

string X, Y;

int main(){
  int n;
  int dp[1002][1002];
  cin >> n;
  for (int a=0;a<n;a++){
    cin >> X>> Y;
    X = ' ' + X; Y = ' ' + Y;
    int maxlcs=0;
    for (int i=0;i<=max(X.length(), Y.length());++i)
      dp[i][0] = dp[0][i] = 0;
    for (int i=1;i<=X.length();++i){
      for (int j=1;j<=Y.length();++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]);
          maxlcs = max(maxlcs,dp[i][j]);
      }
    }
    cout << maxlcs-1 << endl;
  }
  return 0;
}

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

static const int N=1000;
int lcs(string X,string Y){
	int c[N+1][N+1];
	int m=X.length();
	int n=Y.length();
	int maxl=0;
	X= " " +X;
	Y= " " +Y;
	for (int i = 0; i <= m; ++i) {c[i][0]=0;	}
	for (int j = 0; 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;
	cin>>s2;
	cout<<lcs(s1,s2)<<endl;
}
	return 0;
}

C++: #include<iostream>
using namespace std;
int solve(string x, string y){
	x=' '+x;
	y=' '+y;
	int n=x.length();
	int m=y.length();
	int dp[n+1][m+1];
	int k=0;
	for(int i=0; i<n+1; i++)
		dp[i][0]=0;
	for(int i=0; i<m+1; i++)
		dp[0][i]=0;
	for(int i=1; i<n+1; i++){
		for(int j=1; j<m+1; 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]);
			k=max(k, dp[i][j]);
		}
	}
	return k-1;
}
int main(){
	int n;
	cin>>n;
	while(n--){
		string x,y;
		cin>>x>>y;
		cout<<solve(x, y)<<endl;
	}
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int q; cin >> q;
    REP(i,q){
        string X,Y; cin >> X >> Y;
        int dp[1005][1005] = {};
        for(int i = 0; i < X.length(); i++){
            for(int j = 0; j < Y.length(); j++){
                if(X[i] == Y[j]){
                    dp[i+1][j+1] = dp[i][j] + 1;
                }
                else{
                    dp[i+1][j+1] = max(dp[i][j+1],dp[i+1][j]);
                }
            }
        }
        cout << dp[X.length()][Y.length()] << endl;
    }

}

C++: #include <iostream>
#include <string>
#include <algorithm>
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 q;
  cin >> q;

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

  return 0;
}

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

int lcs(std::string str1, std::string str2) {
    std::vector<std::vector<int> > table(str1.size(), std::vector<int>(str2.size(), 0));
    // Initialization
    for (int i=0; i<str1.size(); i++) {
        if ( str1[i] == str2[0] ) {
            table[i][0] = 1;
        } else {
            table[i][0] = 0;
        }
    }
    for (int j=0; j<str2.size(); j++) {
        if ( str2[j] == str1[0] ) {
            table[0][j] = 1;
        } else {
            table[0][j] = 0;
        }
    }
    // Calculate LCS
    for (int i=1; i<str1.size(); i++) {
        for (int j=1; j<str2.size(); j++) {
            table[i][j] = std::max( table[i-1][j], table[i][j-1] );
            if ( str1[i] == str2[j] ) {
                table[i][j]++;
            }
        }
    }
    return table[str1.size()-1][str2.size()-1];
}

int main() {
    int n;
    std::cin >> n;
    std::string str1, str2;
    std::vector<int> res(n);
    for (int i=0; i<n; i++) {
        std::cin >> str1 >> str2;
        res[i] = lcs(str1, str2); 
    }
    for (int i=0; i<n; i++) {
        std::cout << res[i] << std::endl;
    }
}

C++: #include<cstdio>
#include<vector>
#include<algorithm>
#include<utility>
#include<numeric>
#include<iostream>
#include<array>
#include<string>
#include<sstream>
#include<stack>
#include<queue>
#include<list>
#define _USE_MATH_DEFINES

#include<math.h>
#include<map>

#define SENTINEL 1000000001

#define min(a,b) (a)>(b)?(b):(a)
#define max(a,b) (a)>(b)?(a):(b)

using namespace std;

int LCS[1010][1010];

void solve(string x, string y)
{
	for (int i = 0; i != x.size(); i++)
	{
		for (int j = 0; j != y.size(); j++)
		{
			if (x[i] == y[j])
			{
				LCS[i + 1][j + 1] = LCS[i][j] + 1;
			}
			else
			{
				LCS[i + 1][j + 1] = max(LCS[i][j + 1], LCS[i + 1][j]);
			}
		}
	}

	cout << LCS[x.size()][y.size()] << endl;
}

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

	for (int i = 0; i < 1000; i++)
	{
		LCS[0][i] = 0;
		LCS[i][0] = 0;
	}

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