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 <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;
}

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

using namespace std;

int lcs (string x, string y) {

    int m, n, c[1001][1001], i, j, maxl = 0;

    m = x.size();
    n = y.size();

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

    for (i = 0; i <= m; i++) {
        c[i][0] = 0;
    }
    for (j = 1; j <= n; j++) {
        c[0][j] = 0;
    }

    for (i = 1; i <= m; i++) {
            for (j = 1; j <= n; j++) {

                if (x[i - 1] == y[j - 1]) {
                    c[i][j] = c[i - 1][j - 1] + 1;
                } else {
                    c[i][j] = max (c[i - 1][j], c[i][j - 1]);
                }

                maxl = max (max, c[i][j]);

            }
    }

    return maxl;

}



int main () {

    int N, i;
    string x, y;

    cin >> N;

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

    }

    return 0;

}