Go: package main

/*
最長共通部分列
*/

import (
	"bufio"
	"bytes"
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

func larger(l, r int) int {
	if l > r {
		return l
	}
	return r
}

var memo [1001][1001]int

func countLCS(x, y string) int {
	x = " " + x
	y = " " + y
	max := 0
	for xi := 1; xi < len(x); xi++ {
		for yi := 1; yi < len(y); yi++ {
			if x[xi] == y[yi] {
				memo[xi][yi] = memo[xi-1][yi-1] + 1
			} else {
				memo[xi][yi] = larger(memo[xi-1][yi], memo[xi][yi-1])
			}
			max = larger(max, memo[xi][yi])
		}
	}
	return max
}

func main() {
	scanner := makeScanner(1024)
	q := eGetInt(scanner)
	for i := 0; i < q; i++ {
		x := eGetLine(scanner)
		y := eGetLine(scanner)
		fmt.Println(countLCS(x, y))
	}
}

////////////////////////////////////////////////////////////////////////////////
// ライブラリ
////////////////////////////////////////////////////////////////////////////////
func makeScanner(maxByte int) *bufio.Scanner {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Buffer(make([]uint8, 0, 8192), maxByte)
	return scanner
}
func makeWordScanner() *bufio.Scanner {
	scanner := makeScanner(8192)
	scanner.Split(bufio.ScanWords)
	return scanner
}
func eGetLine(r *bufio.Scanner) string {
	if r.Scan() {
		return r.Text()
	}
	err := r.Err()
	if err != nil {
		log.Fatal(err)
	}
	// EOF
	return ""
}
func eGetInt(r *bufio.Scanner) int {
	line := eGetLine(r)
	return eAtoi(line)
}
func eGetFields(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Fields(line)
}
func eGetInts(r *bufio.Scanner) []int {
	fields := eGetFields(r)
	ints := make([]int, len(fields))
	for i := 0; i < len(ints); i++ {
		ints[i] = eAtoi(fields[i])
	}
	return ints
}
func eGetChars(r *bufio.Scanner) []string {
	line := eGetLine(r)
	return strings.Split(line, "")
}
func eAtoi(s string) int {
	n, err := strconv.Atoi(s)
	if err != nil {
		log.Fatal(err)
	}
	return n
}

type ints []int

func (s ints) String() string {
	var buf bytes.Buffer
	buf.WriteString("[")
	sep := ""
	for _, v := range s {
		buf.WriteString(fmt.Sprintf("%s%d", sep, v))
		sep = ", "
	}
	buf.WriteString("]")
	return buf.String()
}

func reverse(a ints) ints {
	r := make(ints, len(a))
	for i := 0; i < len(a); i++ {
		r[i] = a[len(a)-1-i]
	}
	return r
}
func max(a ints) (int, bool) {
	if len(a) == 0 {
		return 0, false
	}
	m := a[0]
	for _, e := range a {
		if e > m {
			m = e
		}
	}
	return m, true
}
func min(a []int) int {
	m := a[0]
	for _, v := range a {
		if v < m {
			m = v
		}
	}
	return m
}

func sum(a ints) int {
	if len(a) == 0 {
		return 0
	}
	sum := 0
	for _, e := range a {
		sum += e
	}
	return sum
}
func find(n int, a ints) int {
	for i, v := range a {
		if n == v {
			return i
		}
	}
	return -1
}

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

int main(){
  int n;
  char x[1000],y[1000];
  int dp[1001][1001];
  //load n;
  cin >>n;
  for(int i=0;i<n;i++){
    //load string
    scanf("%s",x);
    scanf("%s",y);
    //dp
    for(int j=0;j<strlen(x);j++){
      for(int k=0;k<strlen(y);k++){
	if(x[j]==y[k]){
	  dp[j+1][k+1]=dp[j][k]+1;
	}

	else{
	  if(dp[j][k+1]>dp[j+1][k]){
	    dp[j+1][k+1]=dp[j][k+1];
	  }

	  else{
	    dp[j+1][k+1]=dp[j+1][k];
	  }


	}
      }
    }
    cout <<dp[strlen(x)][strlen(y)]<<endl;
  }



  return 0;
}

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

#define N 1001
int A[N][N];

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

    string target1;
    string target2;
    for (int i = 0; i < q; i++) {
        cin >> target1;
        cin >> target2;
        int l1 = target1.length();
        int l2 = target2.length();
        for (int m = 0; m <= l1; m++) {
            A[m][0] = 0;
        }
        for (int n = 0; n <= l2; n++) {
            A[0][n] = 0;
        }

        target1 = ' ' +  target1;
        target2 = ' ' +  target2;

        for (int m = 1; m <= l1; m++) {
            for (int n = 1; n <= l2; n++) {
                if (target1[m] == target2[n]) {
                    A[m][n] = A[m-1][n-1] + 1;
                } else {
                    A[m][n] = max(A[m-1][n], A[m][n-1]);
                }
            }
        }

        cout << A[l1][l2] << endl;
    }

    return 0;
}

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

#define iter(n) for(int iter_var = 0; iter_var < n; iter_var += 1)
#define rep(i, s, n) for(int i = s; i < n; i += 1)
#define lli long long int
#define INF 1e9
#define MOD 1000000007

using namespace std;

int lcs(string s, string t){

  int s_size = s.size();
  int t_size = t.size();
  vector<vector<int>> dp(s_size + 1, vector<int>(t_size + 1, 0));

  rep(i, 1, s_size + 1) rep(j, 1, t_size + 1){
    if(s[i - 1] == t[j - 1]){
      dp[i][j] = dp[i - 1][j - 1] + 1;
    } else {
      dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  return dp[s_size][t_size];
}

int main(){

  int n; cin >> n;
  iter(n){
    string s, t; cin >> s >> t;
    cout << lcs(s, t) << endl;
  }

  return 0;
}

C++: #define _USE_MATH_DEFINES

#include <iostream>
#include <iomanip>
#include <iterator>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <cfloat>
#include <queue>
#include <stack>
#include <string>
#include <map>
#include <functional>
#include <stdexcept>

typedef long long ll;
typedef unsigned int ui;
typedef unsigned short us;
typedef unsigned long long ull;

template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<std::vector<T>>& v) {
  if (!v.empty()) {
    for (std::vector<T> v2 : v) {
      std::copy(v2.begin(), v2.end(), std::ostream_iterator<T>(out, ", "));
      out << "\b\b \n";
    }
  }
  return out;
}
template <typename T>
std::ostream& operator<< (std::ostream& out, const std::vector<T>& v) {
  if ( !v.empty() ) {
    std::copy (v.begin(), v.end(), std::ostream_iterator<T>(out, ", "));
    out << "\b\b ";
  }
  return out;
}

using namespace std;

namespace my {
  class LCS
  {
  public:
    LCS() = default;
    ~LCS() = default;

  private:

  };
}

int main(int argc, const char * argv[]) {
  int q;
  cin >> q;
  vector<string> x;
  vector<string> y;
  for (size_t i = 0; i < q; i++)
  {
    string temp;
    cin >> temp;
    x.push_back(temp);
    cin >> temp;
    y.push_back(temp);
  }
  
  
  
  for (size_t i = 0; i < q; i++)
  {
    size_t x_length = x[i].length();
    size_t y_length = y[i].length();
    size_t max_length = min(x_length, y_length);
    vector<int> length_y_nanmoji(1, y_length);
    map<char, vector<int>> y_nanmoji;
    for (size_t j = 0; j < x_length; j++)
    {
      char x_char = x[i][j];
      if (y_nanmoji.find(x_char) == y_nanmoji.end()) {
        size_t count = 0;
        bool is_looped = false;
        do
        {
          count = y[i].find(x_char, count);
          if (count >= y_length) {
            if (!is_looped) {
              y_nanmoji[x_char].push_back(-1);
            }
            break;
          }
          else {
            y_nanmoji[x_char].push_back((int)count);
            count++;
          }
        } while (is_looped = true, true);
      }

      auto y_nanmoji_now = y_nanmoji[x_char];
      if (y_nanmoji_now[0] == -1) {
        continue;
      }
      for(int l = (int)y_nanmoji_now.size() - 1; l >= 0; l--)
      {
        int val = y_nanmoji_now[l];
        bool is_done = false;
        for (int k = 0; k < (int)length_y_nanmoji.size(); k++)
        {
          if (length_y_nanmoji[k] >= val) {
            length_y_nanmoji[k] = val;
            is_done = true;
            break;
          }
        }
        if (!is_done) {
          length_y_nanmoji.push_back(val);
        }
      }

    }
    int kotae = length_y_nanmoji[0] >= y_length ? 0 : length_y_nanmoji.size();
    cout << kotae << endl;
  }

  return 0;
}