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

Python: for i in range(int(input())):
    s=input()
    t=input()
    a=[0]
    for i in s:
        for j in range(len(a)-1,-1,-1):
            b=t.find(i,a[j])+1
            if b:
                if j+1<len(a):
                    a[j+1]=min(a[j+1],b)
                else:
                    a.append(b)
    print(len(a)-1)

Python: def lcs(x, y):
    lx = len(x)
    ly = len(y)
    pcsl = [[0 for j in range(ly + 1)] for i in range(lx + 1)]
    for i in range(lx):
        for j in range(ly):
            if x[i] == y[j]:
                pcsl[i][j] = pcsl[i - 1][j - 1] + 1
            else:
                pcsl[i][j] = max(pcsl[i - 1][j], pcsl[i][j - 1])
    print(pcsl[i][j])


q = int(input())

for i in range(q):
    ax = input()
    ay = input()
    lcs(ax, ay)

Python: #! /usr/bin/env python3
# -*- coding: utf-8 -*-

from sys import setrecursionlimit
from functools import lru_cache


def lcs(s1, s2):
    cost = [0]
    for c in s2:
        #print("c=", c)
        for i in reversed(range(len(cost))):
            index = s1.find(c, cost[i]) + 1
            if index:
                if i + 1 < len(cost):
                    cost[i + 1] = min(cost[i + 1], index)
                else:
                    cost.append(index)
            #print("i={}, index={}, cost={}".format(i, index, cost))
    return len(cost) - 1


def main():
    setrecursionlimit(10000)

    q = int(input())

    for _ in range(q):
        x = input()
        y = input()
        print(lcs(x, y))


if __name__ == '__main__':
    # print("TCCAGATGG")
    # print("TCACA")
    #print(lcs("TCCAGATGG", "TCACA"))
    main()

Python: for _ in[0]*int(input()):
 X,z=input(),[]
 for y in input():
  s=i=0
  for k in z:
   t=X.find(y,s)+1
   if not t:break
   if t<k:z[i]=t
   s=k;i+=1
  else:
   t=X.find(y,s)+1
   if t:z+=[t]
 print(len(z))

Python: def lcs(s1_raw, s2_raw):
    dp = []
    for s2 in s2_raw:
        start = 0
        for i, cnt in enumerate(dp):
            index = s1_raw.find(s2, start) + 1
            if not index:
                break
            else:
                dp[i]  = min(cnt , index)
                start = cnt
        index = s1_raw.find(s2, start) + 1
        if index:
            dp.append(index)
    return len(dp)


if __name__ == "__main__":
    q = int(input())
    for _ in range(q):
        x = input()
        y = input()
        print(lcs(x, y))

Python: def lcs(s1, s2):
    dp = []
    for s2_k in s2:
        bgn_idx = 0
        for i, cur_idx in enumerate(dp):
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if not chr_idx:
                break
            dp[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        else:
            chr_idx = s1.find(s2_k, bgn_idx) + 1
            if chr_idx:
                dp.append(chr_idx)
    return len(dp)


n = int(input())
for _ in range(n):
    x = input()
    y = input()
    print(lcs(x, y))

Python: trial = int(input())
for t in range(trial):
    first = input()
    second = input()
    dp = [[0 for n in range(len(second) + 1)] for m in range(len(first) + 1)]
    for fi in range(len(first) + 1):
        for se in range(len(second) + 1):
            if fi == 0 or se == 0:
                continue
            else:
                if first[fi-1] == second[se-1]:
                    dp[fi][se] = dp[fi-1][se-1] + 1
                else:
                    dp[fi][se] = max(dp[fi-1][se],dp[fi][se-1],dp[fi-1][se-1])
    print(dp[-1][-1])

Python: # -*- coding: utf-8 -*-
def lcs_sol(x, y):
    n = len(x)
    m = len(y)
    dp = [0 for j in range(m + 1)]

    for i in range(1, n + 1):
        dp_prev = dp[:]
        for j in range(1, m + 1):
            if x[i - 1] == y[j - 1]:
                dp[j] = dp_prev[j - 1] + 1
            else:
                dp[j] = max(dp_prev[j], dp[j - 1])

    return dp[m]


if __name__ == '__main__':
    import sys
    q = int(sys.stdin.readline().strip())
    for i in range(q):
        x = sys.stdin.readline().strip()
        y = sys.stdin.readline().strip()
        print(lcs_sol(x, y))

Python: def lcs(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)
    for i in range(a):
        e1 = x[i]
        c2 = c1[:]#スライス
        for j in range(b):
            if e1 == y[j]:
                c1[j+1] = c2[j] + 1
            elif c1[j+1] < c1[j]:
                c1[j+1] = c1[j]
    return c1[-1]
    
n = int(input().rstrip())#rstripで余計な空白削除
ret = []
for i in range(n):
    s1 = input().rstrip()
    s2 = input().rstrip()
    ret.append(lcs(s1, s2))
print(*ret, sep="\n")

Python: t=int(input())
for _ in range(t):
    s=str(input())
    t=str(input())
    dp = [[0 for i in range(len(t)+1)] for j in range(len(s)+1)]
    for i in range(len(s)):
        for j in range(len(t)):
            if s[i]==t[j]:
                dp[i+1][j+1]=dp[i][j]+1
            else:
                dp[i+1][j+1]=max(dp[i][j+1],dp[i+1][j])
    print(dp[len(s)][len(t)])

Python: 
def LCS(x, y):
    dp = {}
    for i in range(len(x)+1):
        dp[i] = {0: i}
    for j in range(len(y)+1):
        dp[0][j] = j
    
    for i in range(1, len(x)+1):
        for j in range(1, len(y)+1):
            d = 0 if x[i-1] == t[j-1] else 1
            dp[i][j] = min(dp[i-1][j] + 1, dp[i][j-1] + 1, dp[i-1][j-1] + d)
    
    longer = max(len(x), len(y))
    return (longer - dp[len(x)][len(y)])

q = int(raw_input())
for i in range(q):
    X = raw_input()
    Y = raw_input()
    print(LCS(X, Y))

Python: q = int(input())

def lcs(a, b):
    # L[i]はa1~ajとb1~bkから長さがi+1の共通部分列が作れるときのjが取りうる最小値とする
    L = []
    for bk in b:
        bgn_idx = 0 # 検索開始位置
        for i, cur_idx in enumerate(L):
            # bkがbgn_idx以降でaにあれば、そのidx+1かcur_idxの小さい方で更新
            chr_idx = a.find(bk, bgn_idx) + 1
            if not chr_idx:
                break
            L[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        # 全部まわったということはbkが現在の最長の共通文字列以降にaにあったということ。であれば末尾に追加できる
        else:
            # bkがbgn_idx以降でaにあれば、そのidx+1をLに追加
            chr_idx = a.find(bk, bgn_idx) + 1
            if chr_idx:
                L.append(chr_idx)
    return len(L)

for _ in range(q):
    s = input()
    t = input()
    print(lcs(s, t))