Go: package main

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

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func main() {
	fp := os.Stdin
	if len(os.Args) > 1 {
		fp, _ = os.Open(os.Args[1])
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(os.Stdout)

	n := getNextInt(scanner)
	for i := 0; i < n; i++ {
		x := getNextString(scanner)
		y := getNextString(scanner)

		fmt.Fprintln(writer, lcs(x, y))
	}

	writer.Flush()
}

func lcs(x, y string) int {
	lx := len(x)
	ly := len(y)
	dp := make([][]int, lx+1)
	for i := 0; i <= lx; i++ {
		dp[i] = make([]int, ly+1)
	}
	for i := 0; i < lx; i++ {
		for ii := 0; ii < ly; ii++ {
			if x[i] == y[ii] {
				dp[i+1][ii+1] = dp[i][ii] + 1
				continue
			}
			dp[i+1][ii+1] = dp[i][ii+1]
			if dp[i+1][ii] > dp[i+1][ii+1] {
				dp[i+1][ii+1] = dp[i+1][ii]
			}
		}
	}
	return dp[lx][ly]
}

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], maxl = 0, i, j;
  int m = x.size();
  int n = y.size();

  x = ' ' + x;
  y = ' ' + y;
 
  for(i = 1; i <= m; i++) c[i][0] = 0;
  for(i = 0; i <= n; i++) c[0][i] = 0;

  for(i = 1; i <= m; i++){
    for(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 x, y;
  int q, i;

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

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;

int lcs(string x, string y) {
  vector<vector<int> > dp(x.size() + 1, vector<int>(y.size() + 1));
  int xl = x.size();
  int yl = y.size();
  int maxl = 0;
  x = ' ' + x;
  y = ' ' + y;

  for (int i = 0; i <= xl; i++) {
    dp[i][0] = 0;
  }
  for (int j = 1; j <= yl; j++) {
    dp[0][j] = 0;
  }

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

int main() {
  int q;
  cin >> q;
  string x, y;
  rep(i, q) {
    cin >> x >> y;
    cout << lcs(x, y) << endl;
  }
  return 0;
}

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

int CS(string a, int lena, string b, int lenb)
{
    int ret;

    if ((lena > 0) && (lenb > 0)) {
        if (a[lena] == b[lenb]) {
            ret = CS(a, lena - 1, b, lenb - 1) + 1;
        }
        else {
            ret = max(CS(a, lena, b, lenb - 1), CS(a, lena - 1, b, lenb));
        }
    }
    else {
        ret = 0;
    }

    return ret;
}

int LCS(string a, string b)
{
    int ret;
    int len1, len2;

    len1 = a.length();
    len2 = b.length();
    ret  = CS(a, len1, b, len2);

    return ret;
}

int main(void)
{
    int i;
    int num;
    int ret;
    string a, b;

    scanf("%d", &num);
    for (i = 0; i < num; i++) {
        cin >> a;
        cin >> b;
        ret = LCS(a, b);
        printf("%d\n", ret);
    }

    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int main(){
  char str1[1000],str2[1000];
  int LCS[1000][1000]={0};
  int length1,length2;
  int n;
  ios::sync_with_stdio(false);
  scanf("%d",&n);
  for(int s=1;s<=n;s++){
   while(cin>>str1>>str2){
     length1=strlen(str1);
     length2=strlen(str2);
     for(int i=0;i<=length1-1;i++){
       LCS[i][0]=0;
     }
     for(int j=0;j<=length2-1;j++){
       LCS[0][j]=0;
     }
     for(int i=1;i<=length1;i++){
       for(int j=1;j<=length2;j++){
         if(str1[i-1]==str2[j-1]) {
           LCS[i][j]=LCS[i-1][j-1]+1;
         }
         else LCS[i][j]=max(LCS[i-1][j],LCS[i][j-1]);
       }
     }
     printf("%d\n",LCS[length1][length2]);
   }
 }
   return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

using Lint=long long;

void solve()
{
   string s,t; cin>>s>>t;
   int N=s.size(),M=t.size();
   s=' '+s; t=' '+t;
   vector<vector<int>> dp(N+1,vector<int>(M+1));
   for(int i=1;i<=N;i++) for(int j=1;j<=M;j++){
      if(s[i]==t[j]){ dp[i][j]=dp[i-1][j-1]+1; }
      else{ dp[i][j]=max(dp[i-1][j],dp[i][j-1]); }
   }

   string ans;
   int i=N,j=M;
   while(dp[i][j]>0){
      if(s[i]==t[j]){
         ans=s[i]+ans;
         i--; j--;
      }else{
         if(dp[i-1][j]>dp[i][j-1]) i--;
         else j--;
      }
   }
   // cout<<ans<<endl;
   cout<<ans.size()<<endl;
}

int main()
{
   int Q; cin>>Q;
   while(Q--){ solve(); }
   return 0;
}

C++: #include <iostream>

int max(int x, int y)
{
	return x>y ? x : y;
}

int main()
{
	std::string strq; getline(std::cin, strq);
	int q = std::stoi(strq);
	
	for (int i=0; i<q; i++) {
		std::string X; getline(std::cin, X);
		std::string Y; getline(std::cin, Y);
		
		int n = X.size();
		int m = Y.size();
		
		int** DP = new int*[n+1];
		for (int i=0; i<n+1; i++) DP[i] = new int[m+1];
	
		for (int i=0; i<n+1; i++) DP[i][0] = 0;
		for (int j=1; j<m+1; j++) DP[0][j] = 0;
	
		for (int i=1; i<n+1; i++) {
			for (int j=1; j<m+1; j++) {
				if (X[i-1] == Y[j-1]) DP[i][j] = DP[i-1][j-1] + 1;
				else DP[i][j] = max(DP[i][j-1], DP[i-1][j]);
			}
		}
		
		std::cout << DP[n][m] << std::endl;

		for (int i=0; i<n+1; i++) delete[] DP[i];
		delete[] DP;
	}
	
	return 0;
}

C++: import java.util.Scanner;

public class alds1_10 {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String X = null, Y = null;
        int[][] dp = new int[1001][1001];

        int q = sc.nextInt();

        for (int i = 0; i < q; ++i) {
            X = sc.next();
            Y = sc.next();
            for (int x = 0; x < X.length(); ++x) {
                for (int y = 0; y < Y.length(); ++y) {
                    if (X.charAt(x) == Y.charAt(y)) {
                        dp[x + 1][y + 1] = dp[x][y] + 1;
                    }
                    else {
                        if (dp[x + 1][y] > dp[x][y + 1]) {
                            dp[x + 1][y + 1] = dp[x + 1][y];
                        }
                        else {
                            dp[x + 1][y + 1] = dp[x][y + 1];
                        }
                    }
                }
            }
            System.out.printf("%d\n", dp[X.length()][Y.length()]);
        }
    }
}

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

int main() {
    int q, max_num, now_max, start_pos, target_pos;
    char target;
    string X, Y;

    cin >> q;

    for (int i = 0; i < q; i++) {
        cin >> X >> Y;
        max_num = 0;
        for (int j = 0; j < Y.length(); j++) {
            start_pos = j;
            target = Y[start_pos];
            target_pos = 0;
            now_max = 0;
            while (start_pos < Y.length()) {
                for (int k = target_pos; k < X.length(); k++) {
                    if (target == X[k]) {
                        now_max++;
                        target = Y[start_pos];
                        target_pos = k + 1;
                        break;
                    }
                }
                start_pos++;
                target = Y[start_pos];
            }
            max_num = max(max_num, now_max);
        }
        cout << max_num << endl;
    }
}