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 <bits/stdc++.h>
using namespace std;

int main() {

  int q;
  cin >> q;
  
  while (0 < q) {
    
    string X, Y;
    cin >> X >> Y;

    int x = X.size();
    int y = Y.size();

    // X_0 ... X_i と Y_0 ... Y_j に対するLCSの長さ
    vector<vector<int>> dp(x+1, vector<int>(y+1));

    int answer = 0;

    for (int i = 0; i < x; i++) {
      for (int j = 0; j < y; j++) {
        if (X[i] == Y[j]) {
          // 同じ文字列の場合は、
          // X_0 ... X_i と Y_0 ... Y_j に対するLCSに1加える
          dp[i+1][j+1] = dp[i][j] + 1;
        } else {
          // 違う場合は、
          // X_0 ... X_i と Y_0 ... Y_j+1 に対するLCSと
          // X_0 ... X_i+1 と Y_0 ... Y_j に対するLCSで
          // 大きい方
          dp[i+1][j+1] = max(dp[i][j+1], dp[i+1][j]);
        }
        answer = max(answer, dp[i+1][j+1]);
      }
    }

    cout << answer << endl;

    q--;
  }

  return 0;

}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<deque>
#include<map>
#include<stack>
#include<set>
#include<utility>
using namespace std;
typedef pair<int,int> ii;
typedef long long ll;
typedef pair<ll,ii> P;
typedef unsigned long long int ull;
const int MOD=1e9+7;
int dy[]={1,0,-1,0};
int dx[]={0,1,0,-1};
const int MAXN=100000;
const int MAXE=100000;
const int MAXV=10000;
const ll INF=2e18;

int main(){
    int q;cin>>q;
    int dp[1001][1001];
    for(int k=0;k<q;k++){
        string s,t;cin>>s>>t;
        int n=(int)s.size();
        int m=(int)t.size();
        for(int i=0;i<=n;i++){
            for(int j=0;j<=m;j++){
                dp[i][j]=0;
            }
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                dp[i+1][j+1]=max(dp[i+1][j],dp[i][j+1]);
                if(s[i]==t[j]) dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j]+1);
            }
        }
        cout<<dp[n][m]<<endl;
    }
    return 0;
}

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

using namespace std;

int dp[1010][1010];



int solve() {

	string s,t;

	cin >> s >> t;


	int max_s = s.length();
	int max_t = t.length();

	memset(dp,0, sizeof(int) * 1010 * 1010);



	for (int i = 1; i <= max_s; i++) {
		for (int j = 1; j <= max_t; j++) {
			if (s.at(i - 1) == t.at(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[max_s][max_t];
}

int main() {
	int q;

	cin >> q;

	for (int i = 0; i  < q; i++) {
		cout << solve() << endl;
	}

}

C++: #include <bits/stdc++.h>
#define INF 100000

using namespace std;
typedef long long int ll;

int dh[4] = {1,0,-1,0};
int dw[4] = {0,1,0,-1};

int main()
{
    int q;
    cin >> q;
    for(int i = 0; i < q; i++)
    {
        string X,Y;
        cin >> X >> Y;
        int lx = X.length();
        int ly = Y.length();
        vector<vector<int>>dp(lx+1,vector<int>(ly+1,0));
        for(int i = 1; i <= lx; i++)
        {
            for(int j = 1; j <= ly; j++)
            {
                if(X[i-1] == Y[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
                else dp[i][j] = max(dp[i-1][j],dp[i][j-1]);
            }
        }
        cout << dp[lx][ly] << endl;
    }
}

C++: #include <iostream>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cmath>
#include <cstdarg>
#include <cstdlib>
#include <string>

#include <vector>
#include <tuple>
#include <stack>
#include <queue>
#include <algorithm>
using namespace std;

int nxtx[] = {1, 0};
int nxty[] = {0, 1};

int main(int argc, char const *argv[]) {
    int q;
    cin >> q;
    string X, Y;
    typedef pair<int, int> pint;
    for (int k = 0; k < q; k ++) {
        cin >> X;
        cin >> Y;
        int lenX = X.size();
        int lenY = Y.size();
        int dp[lenX + 1][lenY + 1];
        for (int i = 0; i <= lenX; i ++) {
            dp[i][0] = 0;
        }
        for (int i = 0; i <= lenY; i ++) {
            dp[0][i] = 0;
        }

        for (int i = 1; i <= lenX; i ++) {
            for (int j = 1; j <= lenY; j ++) {
                if (X[i-1] == Y[j-1]) {
                    dp[i][j] = dp[i-1][j-1] + 1;
                } else {
                    dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
                }
            }
        }
        cout << dp[lenX][lenY] << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>

int main(){
  int q;
  scanf("%d",&q);
  std::vector<std::string > X(q);
  std::vector<std::string > Y(q);
  for (int i=0; i<q; i++){
    std::cin >> X[i];
    std::cin >> Y[i];
  }
  int val[1002][1002];
  for (int k=0; k<q; k++){
    for (int i=0; i<X[k].length(); i++){
      for (int j=0; j<Y[k].length(); j++){
        if (X[k][i]==Y[k][j]){
          if (i==0 && j==0){
            val[i][j]=1;
          }
          if (i>0 && j>0){
            val[i][j]=val[i-1][j-1]+1;
          } else {
            val[i][j]=1;
          }
        } else if (i==0 && j==0){
          val[i][j]=0;
        } else if (i==0){
          val[i][j]=val[i][j-1];
        } else if (j==0){
          val[i][j]=val[i-1][j];
        } else {
          val[i][j]=std::max(val[i-1][j], val[i][j-1]);
        }
      }
    }
    int length = val[X[k].length()-1][Y[k].length()-1];
    printf("%d\n", length);
  }
  return 0;
}