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 <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define N 1050
//LCS

using namespace std;

int LCS (string x, string y) {
    
    int maxl = 0;
    int str[N+1][N+1]; 
    int m = x.size();
    int n = y.size();
    x = ' ' + x;
    y = ' ' + y;

    //初期化
    for (int i = 0; i <= m; i++) {
        str[i][0] = 0;
    }
    for (int i = 1; i <= n; i++) {
        str[0][i] = 0;
    }


    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++){
        	if(x[i] == y[j]){
        	    str[i][j] = str[i - 1][j - 1] + 1;
        	}else{
        	    str[i][j] = max(str[i][j - 1], str[i - 1][j]);
        	}
        	maxl = max(maxl, str[i][j]);
        }
    }
    return maxl;
}

int main() {
    int q;
    cin >> q;
    string X, Y;
    for(int i = 0;i < q; i++) {
        cin >> X >> Y;
        cout << LCS(X,Y) << 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 max1 = 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]);
		}
		max1 = max(max1,C[i][j]);
		}
	} 
	return max1;
}



int main(){
	int q;
	string s1,s2;
	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;

int lcs(string X,string Y){
  //staticにしないと実行時にスタックオーバーフローになる
  static int c[1001][1001];
  int m = X.size();
  int n = Y.size();
  int maxl = 0;
  X = ' ' + X;
  Y = ' ' + Y;
  for(int i=0;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(){
  int q;cin>>q;
  while(q--){
    string a,b;cin>>a>>b;
    cout << lcs(a,b) << endl;
  }
  return 0;
}

C++: #include<bits/stdc++.h>
#define ll long long int
#define FOR(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n)
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;
  REP(i, m + 1) c[i][0] = 0;
  REP(i, n + 1) c[0][j] = 0;

  REP1(i, m + 1) {
    REP1(j, n + 1) {
      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;
  REP(i, n) {
    cin >> s1 >> s2;
    cout << Lcs(s1, s2) << endl;
  }
  return 0;
}

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

int dp[1005][1005];

int lcs(string x, string y){
    for (int i = 1; i <= x.size(); i++){
        for (int j = 1; j <= y.size(); j++)
        {
            if(x[i-1] != y[j-1])
                dp[i][j] = dp[i-1][j] > dp[i][j-1] ? dp[i-1][j] : dp[i][j-1];
            else
                dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[x.size()][y.size()];
}

int main()
{
    int n; cin >>n;
    string x, y;
    
    for (int i = 0; i < n; i++)
    {
        cin >> x >> y;
        cout << lcs(x, y) << endl;
    }
}

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

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

  for(int i = 0; i < q; i++){
    string a, b;
    cin >> a;
    cin >> b;

    vector<vector<int> > dp(a.size() + 1, vector<int>(b.size() + 1, 0));
    for(int i = 0; i < (int)a.size(); i++){ dp[i][0] = 0; }
    for(int i = 0; i < (int)b.size(); i++){ dp[0][i] = 0; }

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

    cout << dp[a.size()][b.size()] << endl;
  }
  
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
constexpr int di[] = { 0, 1, 0, -1 }, dj[] = { 1, 0, -1, 0 };
#define rep(i, n) for (int i = 0; i < n; i++)
#define repr(i, n) for (int i = n; i >= 0; i--)
#define SORT(v) sort((v).begin(), (v).end())
#define SORTR(v) sort((v).rbegin(), (v).rend())
#define all(v) (v).begin(), (v).end()
#define vmax 10000
constexpr ll inf = 100000000000000;
/*cout<<fixed<<setprecision(20);cin.tie(0);ios::sync_with_stdio(false);*/

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;//x[0]に空白を挿入
  y=' '+y;//y[0]に空白を挿入
  for(int i=0;i<=m;i++) c[i][0]=0;
  for(int i=1;i<=n;i++) c[0][i]=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;
  rep(i,n){
    cin>>s1>>s2;
    cout<<lcs(s1,s2)<<endl;
  }
  return 0;
}

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

using namespace std;

int main(void)
{
    int q;
    char x[1001], y[1001];
    int table[1001][1001];
    
    cin >> q;
    for (int i = 0; i < q; i++) {
	cin >> x >> y;
	int xl = strlen(x), yl = strlen(y);

	for (int i = 0; i < xl; i++)
	    for (int j = 0; j < yl; j++)
		if (x[i] == y[j])
		    table[i+1][j+1] = 1 + table[i][j];
		else
		    table[i+1][j+1] = max(table[i][j+1], table[i+1][j]);

	cout << table[xl][yl] << endl;
    }

    return 0;
}

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

int Longest(string x, string y) {
  int L[1001][1001];
  int maxl = -1;
  int xlen = x.size();
  int ylen = y.size();

  x = ' ' + x;
  y = ' ' + y;

  for (int i = 1; i <= xlen; i++) L[i][0] = 0;
  for (int j = 1; j <= ylen; j++) L[0][j] = 0;

  for (int i = 1; i <= xlen; i++) {
    for (int j = 1; j <= ylen; j++) {
      if (x[i] == y[j])
        L[i][j] = L[i-1][j-1] + 1;
      else
        L[i][j] = max(L[i][j-1], L[i-1][j]);

      maxl = max(maxl, L[i][j]);
    }
  }

  return maxl;
}

int main() {
  string X, Y;
  int n;
  int result[1001];

  cin >> n;

  for (int i = 0; i < n; i++) {
    cin >> X;
    cin >> Y;
    result[i] =  Longest(X, Y);
    cout << result[i] << endl;
  }

  


  return 0;
}