Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	Q := sc.nextInt()
	for q := 0; q < Q; q++ {
		X, Y := sc.nextStr(), sc.nextStr()
		xLen, yLen := len(X), len(Y)
		dp := make([][]int, xLen+1)
		for i := 0; i <= xLen; i++ {
			dp[i] = make([]int, yLen+1)
		}
		for x := 0; x <= xLen; x++ {
			for y := 0; y <= yLen; y++ {
				if x == 0 || y == 0 {
					dp[x][y] = 0
				} else if X[x-1] == Y[y-1] {
					dp[x][y] = dp[x-1][y-1] + 1
				} else {
					dp[x][y] = max(dp[x-1][y], dp[x][y-1])
				}
			}
		}
		fmt.Fprintln(wtr, dp[xLen][yLen])
	}

	wtr.Flush()
}

C++: #include <iostream>
#include <fstream>
#include <stdio.h>
#include <algorithm>
#include <string>

#define MAX 10000

using namespace std;

int n;
int LCS[MAX][MAX];

void solve(string a, string b){
    int m = a.size();
    int n = b.size();
    int maxl = 0;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){
            LCS[i][j] = 0;
        }
    }
    bool tmp_ok = false;
    for(int i=0; i<n; i++){
        if(a[0] == b[i] || tmp_ok){
            LCS[i][0] = 1;
            maxl = 1;
            tmp_ok = true;
        }
    }
    tmp_ok = false;
    for(int i=0; i<m; i++){
        if(a[i] == b[0] || tmp_ok){
            LCS[0][i] = 1;
            maxl = 1;
            tmp_ok = true;
        }
    }
    for(int i=1; i<n; i++){
        for(int j=1; j<m; j++){
            if(a[j] == b[i]){
                LCS[i][j] = LCS[i-1][j-1]+1;
            }
            else if(LCS[i-1][j]>=LCS[i][j-1]){
                LCS[i][j] = LCS[i-1][j];
            }
            else LCS[i][j] = LCS[i][j-1];
            maxl = max(maxl, LCS[i][j]);
        }
    }
    
    cout << maxl << endl;
}

int main(int argc, const char * argv[]) {
    // input from txt
    
    /////////
    //write//
    /////////
    
    cin >> n;
    for(int i=0; i<n; i++){
        string str_a;
        string str_b;
        cin >> str_a >> str_b;
        solve(str_a, str_b);
    }
    
    return 0;
}

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++){
    scanf("%s",x);
    scanf("%s",y);
    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+1];
	  }


	}
      }
    }

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

  return 0;
}

C++: #include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n, xl, yl, dp[1003][1003]; char x[1003], y[1003];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; i++) {
		scanf("%s", x); xl = strlen(x);
		scanf("%s", y); yl = strlen(y);
		memset(dp, 0, sizeof(dp));
		for(int i = 1; i <= xl; i++) {
			for(int j = 1; j <= yl; j++) {
				if(x[i - 1] == y[i - 1]) dp[i][j] = dp[i - 1][j - 1] + 1;
				else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
			}
		}
		printf("%d\n", dp[xl][yl]);
	}
}

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

int lengthOfLcs_r(const char x[], const char y[], int ix, int iy, int xsize, int ysize) {
	if (ix == xsize || iy == ysize) {
		return 0;
	}
	if (x[ix] == y[iy]) {
		return lengthOfLcs_r(x, y, ix + 1, iy + 1, xsize, ysize) + 1;
	}
	return max(lengthOfLcs_r(x, y, ix, iy + 1, xsize, ysize), lengthOfLcs_r(x, y, ix + 1, iy, xsize, ysize));
}

int lengthOfLcs(string x, string y) {
	return lengthOfLcs_r(x.c_str(), y.c_str(), 0, 0, x.length(), y.length());
}

int main() {
	int q;
	string x, y;

	cin >> q;
	for (int i = 0; i < q; ++i) {
		cin >> x >> y;
		cout << lengthOfLcs(x, y) << endl;
	}
	return 0;
}

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

void fnInput(vector<string>& rvsStrX, vector<string>& rvsStrY)
{
  int nMaxSiz;
  cin >> nMaxSiz;
  rvsStrX.resize(nMaxSiz);
  rvsStrY.resize(nMaxSiz);

  for (int i = 0; i < nMaxSiz; ++i)
  {
    cin >> rvsStrX[i] >> rvsStrY[i];
    rvsStrX[i] = ' ' + rvsStrX[i];
    rvsStrY[i] = ' ' + rvsStrY[i];
  }
}

int fnLngCmnSubseq(const vector<string>& cnrvsStrX, const vector<string>& cnrvsStrY)
{
  for (int i = 0; i < cnrvsStrX.size(); ++i)
  {
    const int nMAXNX = cnrvsStrX[i].size();
    const int nMAXNY = cnrvsStrY[i].size();
    vector<vector<int>> vvnLcsDP(nMAXNX, vector<int>(nMAXNY, 0));

    for (int nx = 1; nx < nMAXNX; ++nx)
      for (int ny = 1; ny < nMAXNY; ++ny)
        if (cnrvsStrX[i][nx] == cnrvsStrY[i][ny])
          vvnLcsDP[nx][ny] = vvnLcsDP[nx - 1][ny - 1] + 1;
        else
          vvnLcsDP[nx][ny] = max(vvnLcsDP[nx - 1][ny], vvnLcsDP[nx][ny - 1]);

    cout << vvnLcsDP[nMAXNX - 1][nMAXNY - 1] << endl;
  }
}

int main()
{
  cin.tie(0);
  ios::sync_with_stdio(false);
  vector<string> vsStrX, vsStrY;

  fnInput(vsStrX, vsStrY);
  fnLngCmnSubseq(vsStrX, vsStrY);
  return 0;
}

C++: // ????????±?????¨??????
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
#include <set>
#include <string>
#define MAX 1050
using namespace std;
int q,length_x,length_y;
int dp[MAX][MAX];
string X,Y;
int init_func () {
  for (int i=0; i<MAX ; i++)
    for (int j=0; j<MAX ; j++)
      dp[i][j] = 0;
}

int solve () {
  // dp = {0}
  // dp?????¨?????????X????¨????Y??¨??????
  for (int tate=1; tate<=length_x ; tate++)
    for (int yoko=1; yoko<=length_y ; yoko++)
      if (X[tate-1] == Y[yoko-1]) dp[tate][yoko] = 1 + dp[tate-1][yoko-1];
      else dp[tate][yoko] = max(dp[tate-1][yoko],dp[tate][yoko-1]);
  
  return dp[length_x][length_y];
}

int main(){
  cin >> q;
  for (int i=0; i<q; i++) {
    cin >> X >> Y;
    length_x = X.length();
    length_y = Y.length();
    init_func();
    cout << solve() << endl;
  }
  return 0;
}