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 <vector>
#include <algorithm>
#include <string>
using namespace std;
#define rep(i, p, n) for(int i = p; i < n; i++) 

int main() {
    int n;
    cin >> n;
    rep(i, 0, n) {
        string s, t;
        cin >> s >> t;
        int lcs[s.size()+1][t.size()+1];
        rep(j, 0, s.size()+1) {
            rep(k, 0, t.size()+1) {
                lcs[j][k] = 0;
            }
        }
        rep(j, 1, s.size()+1) {
            rep(k, 1, t.size()+1) {
                if(s[j-1] == t[k-1]) {
                    lcs[j][k] = max(lcs[j-1][k], max(lcs[j][k-1], lcs[j-1][k-1]+1));
                }
                else {
                    lcs[j][k] = max(lcs[j-1][k], lcs[j][k-1]);
                }
            }
        }

        cout << lcs[s.size()][t.size()] << endl;
        
    }

    return 0;
}

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

using namespace std;

string s1,s2;

int L(int i,int j){
	if(i<0||j<0)return 0;
	else if(s1.at(i)==s2.at(j))return 1+L(i-1,j-1);
	else{
		int a=L(i,j-1);
		int b=L(i-1,j);
		return a>b?a:b;
	}
}

int main(){
	int q;
	cin>>q;
	for(int i=0; i<q; i++){
		cin>>s1>>s2;
		cout<<L(s1.size()-1,s2.size()-1)<<endl;
	}
}

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

int q;
string X, Y;

int dp[1000][1000];

int solve(int idx_x, int idx_y) {
  if (idx_x < 0 || idx_y < 0)
    return 0;

  if (dp[idx_x][idx_y] != -1)
    return dp[idx_x][idx_y];

  int ret;
  
  if (X[idx_x] == Y[idx_y]) {
    ret = solve(idx_x - 1, idx_y - 1) + 1;
  } else {
    ret = solve(idx_x - 1, idx_y);
    ret = max(solve(idx_x, idx_y - 1), ret);
  }
  
  return dp[idx_x][idx_y] = ret;
}

int main(void) {
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> X >> Y;
    memset(dp, -1, sizeof(dp));
    cout << solve(X.size()-1, Y.size()-1) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;

int q;
char X[1000], Y[1000];
int dp[1001][1001];

int main(void) {

    scanf("%d", &q);

    for (int i = 0; i < q; ++i) {
        scanf("%s %s", X, Y);
        int xlen = strlen(X), ylen = strlen(Y);
        for (int x = 0; x < xlen; ++x) {
            for (int y = 0; y < ylen; ++y) {
                if (X[x] == Y[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];
                    }
                }
            }
        }
        printf("%d\n", dp[xlen][ylen]);
//        memset(dp, 0, sizeof(int)*1001*1001);
    }

    return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;

int calcAns(string s, string t) {
    int r = s.size() + 1;
    int c = t.size() + 1;

    int dp[r][c];
    rep(i, r)rep(j,c) dp[i][j] = 0;

    for(int i = 1; i < r; i++) {
        for(int j = 1; j < c; j++) {
            dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
            if(s[i-1] == t[j-1]) dp[i][j] = dp[i-1][j-1] + 1;
        }
    }
    return dp[r-1][c-1];
}

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

    rep(i, q) {
        string s, t;
        cin >> s >> t;
        int ans = calcAns(s, t);
        cout << ans << endl;
    }
    return 0;
}

C++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem
最長共通部分列

 
Category
DP
 
URL:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_10_C&lang=ja

考察:
dp[X][Y] := Xに対するYのLCSを作るのに必要なXの部分文字列のindexの最小値
--> LCSのサイズの情報がないのでダメ。

dp[X][Y] := XとYで作れる LCS の長さ
--> 素直な定義。でも漸化式が作れるのか？
dp[0][Y] = 0
dp[i][j] = X[i] == Y[j] ? dp[i-1][j-1] + 1 : max(dp[i][j-1], dp[i-1][j])



Date: 2020/04/18
*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int Q;
vector<vector<int>> dp;
vector<int> V;
string X, Y;

/* ======================== */

void getInput() {
    cin>>X;
    cin>>Y;
}
 
 
void solve() {
    dp = vector<vector<int>>(X.size(), vector<int>(Y.size(), 0));

    for (int i=0; i<X.size(); ++i) {
        for (int j=0; j<Y.size(); ++j) {
            int a = (i>0 and j>0) ? dp[i-1][j-1] : 0;
            int b = (j>0) ? dp[i][j-1] : 0;
            int c = (i>0) ? dp[i-1][j] : 0;
            if (X[i] == Y[j]) {
                dp[i][j] = max(a + 1, max(b, c));
            }
            else {
                dp[i][j] = max(b, c);
            }
        }
    }
    // print(dp);
    cout<<dp[X.size()-1][Y.size()-1]<<endl;
}
 
 
 
signed main(){
    cin>>Q;
    for(int i=0; i<Q; ++i) {
        getInput();
        solve();
    }
    return 0;
}