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<bits/stdc++.h>
using namespace std;
int main(){
  int n,memo[1111][1111];
  string x,y;
  memset(memo,0,sizeof(memo));
  cin>>n;
  for(int count=0;count<n;count++){
    cin>>x>>y;
    int h=x.size(),w=y.size();
    for(int i=0;i<=w;i++){
      for(int j=0;j<=h;j++){
	if(i==0||j==0) continue;
	if(x[j-1]==y[i-1]){
	  memo[i][j]=max(memo[i-1][j-1]+1,memo[i][j-1]);
	}else if(x[j-1]!=y[i-1]){
	  memo[i][j]=max(memo[i-1][j],memo[i][j-1]);
	}
      }
    }
    cout<<memo[w][h]<<endl;
  }
  return(0);
}

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 <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
const int INF_INT = 2147483647;
const long long INF_LONG = 9223372036854775807;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)


void f () {
  string s, t;
  cin >> s >> t;
  vector<vector<int>> dp(s.size(), vector<int>(t.size()));
  tuple<int, int, int> tup(0, 0, 0);

  rep (i, t.size()) if(s[0] == t[i]) dp[0][i] = 1;
  rep (i, s.size()) if(t[0] == s[i]) dp[i][0] = 1;
  for (int i=1; i<s.size(); ++i) for (int j=1; j<t.size(); ++j) {
    if (s[i] == t[j]) dp[i][j] = 1;
    if (get<0>(tup) < dp[i][j]) tup = tie(dp[i][j], i, j);
  }

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

  cout << dp[s.size() - 1][t.size() - 1] << endl;
}


int main() {
  int q;  cin >> q;
  
  rep (i, q) f();
}

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();
  
  for (int i = 0; i < m; i++) c[i][0] = 0;
  for (int i = 0; i < n; i++) c[0][j] = 0;

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      if (X[i] == Y[j]) 
        if (i == 0 || j == 0) 
          c[i][j] = 1;
        else 
          c[i][j] = c[i - 1][j - 1] + 1;
      else
        c[i][j] = max(c[i][j - 1], c[i - 1][j]);
    }
  }
  return c[m - 1][n - 1];
}

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++: #include <iostream>
#include <stdio.h>
#include <vector>
#include <math.h>
#include <algorithm>

using namespace std;

int main() {
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        string s, t;
        cin >> s >> t;
        int dp[1001][1001] = {};
        for (int j = 0; j < s.length(); ++j) {
            for (int k = 0; k < t.length(); ++k) {
                if (s[j] == t[k]) {
                    dp[j + 1][k + 1] = max(dp[j + 1][k + 1], dp[j][k] + 1);
                }
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j+1][k]);
                dp[j+1][k+1] = max(dp[j+1][k+1], dp[j][k+1]);
            }
        }
        cout << dp[s.length()][t.length()] << endl;

    }
    return 0;
}

C++: #include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int p[1001][1001]={0};
int main(){
int i,j,q,e,l,z,m,u,n;
char s[2001],c[2001];
cin>>q;
for(e=0;e<q;e++){
scanf("%s %s",s,c);
l=strlen(s);
z=strlen(c);

for(i=0;i<=l;i++){
	for(j=0;j<=z;j++){
		p[i][j]=0;
	}
}
for(i=1;i<=z;i++){
	for(j=1;j<=l;j++){
		if(c[i-1]==s[j-1])p[j][i]=p[j][i-1]+1;
		else p[j][i]=max(p[j][i-1],p[j][i-1]);
	}
}
cout<<p[l][z]<<endl;
}
return 0;
}

C++: #include <iostream>
#include <string>
using namespace std;
int main(void){
    int n;
    cin >> n;
    for(int i = 0;i < n;i++){
        string X,Y;
        cin >> X >> Y;
        int L[X.length()+10][Y.length()+10];
        for(int j = 0;j < X.length()+5;j++){
            for(int k = 0;k < Y.length()+5;k++){
                L[j][k] = 0;
            }
        }
        for(int j = 1;j<=X.length();j++){
            for(int k = 1;k<=Y.length();k++){
                if(Y[k-1] == X[j-1]){
                    L[j][k] = 1+L[j-1][k-1];
                }
                else{
                    L[j][k] = max(L[j-1][k],L[j][k-1]);
                }
            }
        }
        cout << L[X.length()][Y.length()] << endl;
    }
    return 0;
}

C++: #include "iostream"
#include "string.h"

using namespace std;

int L[1001][1001];

int lcs( char s1[], char s2[], int l1, int l2 ) {

	if (L[l1][l2] != 0)return L[l1][l2];

	if (l1 == 0 || l2 == 0) return 0;
	else if (s1[l1-1] == s2[l2-1]) L[l1][l2] = lcs(s1, s2, l1 - 1, l2 - 1) + 1;
	else {
		int lsc1 = lcs(s1, s2, l1 - 1, l2);
		int lsc2 = lcs(s1, s2, l1, l2 - 1);

		if (lsc1 > lsc2) L[l1][l2] = lsc1;
		else L[l1][l2] = lsc2;
	}

	return L[l1][l2];
}

int main() {

	int n;
	char s1[1000], s2[1000];

	cin >> n;

	int ans[150];

	for (int i = 0; i < n; i++) {
		cin >> s1;
		cin >> s2;

		ans[i] = lcs(s1, s2, strlen(s1), strlen(s2));

		for (int j = 0; j < strlen(s1); j++) {
			for (int k = 0; k < strlen(s2); k++) {
				L[j][k] = 0;
			}
		}
	}

	for (int i = 0; i < n; i++) {
		cout << ans[i] << endl;
	}

	return 0;
}