Go: package main

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

type Position struct {
	X int
	Y int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}
	for i := 0; i < n; i++ {
		strX := nextText(sc)
		strY := nextText(sc)
		fmt.Println(calcLcs(strX, strY))
	}
}

func calcLcs(strX, strY string) int {
	return lcs(strX, strY, Position{0, 0}, make(map[Position]int))
}

func lcs(strX string, strY string, position Position, memo map[Position]int) int {
	if position.X >= len(strX) || position.Y >= len(strY) {
		return 0
	}
	if v, ok := memo[position]; ok {
		return v
	}
	if strX[position.X] == strY[position.Y] {
		result := lcs(strX, strY, Position{position.X + 1, position.Y + 1}, memo)
		memo[position] = result + 1
		return memo[position]
	} else {
		result1 := lcs(strX, strY, Position{position.X + 1, position.Y}, memo)
		result2 := lcs(strX, strY, Position{position.X, position.Y + 1}, memo)
		var result int
		if result1 > result2 {
			result = result1
		} else {
			result = result2
		}
		memo[position] = result
		return result
	}
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

func nextText(sc *bufio.Scanner) string {
	sc.Scan()
	return sc.Text()
}

Python: q = int(input())

l = []
x = ""
y = ""
"""
def lcs(yIndex, xIndex):
    global l, x, y
    if xIndex == 0 and yIndex == 0:
        return 0
    elif xIndex == 0:
        return l[yIndex - 1][0]
    elif yIndex == 0:
        return l[0][xIndex - 1]
    elif x[xIndex] == y[yIndex]:
        return 1 + l[yIndex - 1][xIndex - 1]
    else:
        if l[yIndex][xIndex - 1] > l[yIndex - 1][xIndex]:
            return l[yIndex][xIndex - 1]
        else:
            return l[yIndex - 1][xIndex]
        #return max(l[yIndex][xIndex - 1], l[yIndex - 1][xIndex])
 """
for i in range(q):
    x = input()
    y = input()
 
    yLen = len(y)
    xLen = len(x)
    l = [[-1] * xLen for j in range(yLen)]

    for j in range(yLen):
        for k in range(xLen):
            
            if k == 0 and j == 0:
                l[j][k] = 0
            elif k == 0:
                if x[k] == y[j]:
                    l[j][k] = 1
                else:
                    l[j][k] = max(1, l[j - 1][0])
            elif j == 0:
                if x[k] == y[j]:
                    l[j][k] = 1
                else:
                    l[j][k] = max(1, l[0][k - 1])
            elif x[k] == y[j]:
                l[j][k] =  1 + l[j - 1][k - 1]
            else:
                if l[j][k - 1] > l[j - 1][k]:
                    l[j][k] = l[j][k - 1]
                else:
                    l[j][k] = l[j - 1][k]
            #l[j][k] = lcs(j, k)
 
    print(l[yLen- 1][xLen - 1])

Python: def Ics(X, Y):
    m = len(X)
    n = len(Y)
    c1 = [0] * (n + 1)
    for i in range(m):
        d = X[i]
        c2 = c1[:]
        for j in range(n):
            if (d == Y[j]):
                c1[j + 1] = c2[j] + 1
            elif (c1[j + 1] < c1[j]):
                c1[j + 1] = c1[j]
    return (c1[-1])
 
q = int(input())
t = []
for i in range(q):
    s1 = input()
    s2 = input()
    t.append(Ics(s1, s2))
    print(*t)

Python: def lcs(x,y,l,j,k):
	if(x[j-1]==y[k-1]):
		lcs(x,y,l,j-1,k-1)
		global c
		c+=1
	else:
		if l[j-1][k]>=l[j][k-1]:
			lcs(x,y,l,j-1,k)
		else:
			lcs(x,y,l,j,k-1)

q=int(input())
c=0
for i in range(q):
	x=list(input())
	y=list(input())
	l=[[0 for i in range(len(y)+1)] for j in range(len(x)+1)]

for j in range(1, len(x) + 1):
    for k in range(1, len(y) + 1):
        if x[j - 1] == y[k - 1]:
            a = 1
        else:
            a = 0
        l[j][k] = max(l[j-1][k-1] + a, l[j-1][k], l[j][k-1])
lcs(x,y,l,len(x),len(y))
print(c)

Python: a=''
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 t<1:break
   if t<k:z[i]=t
   s=k;i+=1
  else:
   t=X.find(y,s)+1
   if t:z+=[t]
 a+=f'\n{len(z)}'
print(a[1:])

Python: Q = int(input().strip())
for q in range(Q):
    X = str(input().strip())
    Y = str(input().strip())
    lx = len(X)
    ly = len(Y)
    L = [[0 for j in range(0,ly+1)] for i in range(0,lx+1)]
    for i in range(1,lx+1):
        for j in range(1,ly+1):
            if X[i-1] == Y[j-1]:
                L[i][j] = 1 + L[i-1][j-1]
            else:
                L[i][j] = max(L[i-1][j],L[i][j-1])
    print(L[lx][ly])

Python: # Longest Common Subsequence

# 関数に入れる
def main():
    # input ← stdin.readline
    import sys
    input = sys.stdin.readline
    for _ in [0]*int(input()):
        # \n削除
        X = '_' + input()[:-1]
        Y = '_' + input()[:-1]

        lx = len(X)
        ly = len(Y)

        dp = [[0]*ly for _ in [0]*lx]

        # 変数に入れておく
        j_y = tuple(enumerate(Y))
        for i, x in enumerate(X):
            if i == 0:
                continue

            # 変数に入れておく
            dp_i = dp[i]
            dp_i1 = dp[i-1]
    
            for j, y in j_y:
                if x == y:
                    dp_i[j] = dp_i1[j-1] + 1
                else:
                    # max(a, b)を書き下す
                    a = dp_i1[j]
                    b = dp_i[j-1]
                    dp_i[j] = a if a > b else b

        print(dp[-1][-1])

if __name__ == '__main__':
    main()

Python: n = int(input())
dataset = []
for i in range(n):
    tmp = []
    tmp.append(input())
    tmp.append(input())
    dataset.append(tmp)

for data in dataset:
    m = len(data[0])
    n = len(data[1])
    i, j = 0, 0
    dp = []
    for x in range(n+1):
        dp.append([0] *(m+1))

    lcs = 0
    for j in range(n):
        for i in range(m):
            if data[0][i] == data[1][j]:
                dp[j+1][i+1] = dp[j][i] + 1
            else:
                dp[j+1][i+1] = max(dp[j+1][i], dp[j][i+1])

            #if dp[j+1][i+1] > lcs:
            #   lcs = dp[j+1][i+1]
        
    #print(lcs)
    print(dp[n][m])

Python: def lcs(X, Y):
    m, n = len(X), len(Y)
    cost = [[0] * (n+1) for _ in range(m+1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                cost[i][j] = cost[i - 1][j - 1] + 1
            else:
                cost[i][j] = max(cost[i][j - 1], cost[i - 1][j])
    return cost[m][n]

n = int(input())
for _ in range(n):
    X, Y = input(), input()
    print(lcs(X, Y))

Python: def lcs(X, Y):
    C = []
    for y in Y:
        bgn_idx = 0
        for i, cur_idx in enumerate(C):
            chr_idx = X.find(y, bgn_idx) + 1
            if chr_idx == 0:
                break
            C[i] = min(cur_idx, chr_idx)
            bgn_idx = cur_idx
        chr_idx = X.find(y, bgn_idx) + 1
        if chr_idx != 0:
            C.append(chr_idx)
    return len(C)

q = int(input())
for _ in range(q):
    X = input()
    Y = input()
    print(lcs(X, Y))

Python: import sys

def lcm(c,i, j):
    if c[i][j]>0: return c[i][j]        
    if i==0 or j==0:
        return 0
    elif x[i]==y[j]:
        c[i][j] = lcm(c,i-1,j-1)+1
        return c[i][j]
    else:
        c[i][j] = max(lcm(c,i,j-1), lcm(c,i-1,j))
        return c[i][j]

for _ in range(int(sys.stdin.readline())):
    x = sys.stdin.readline()
    y = sys.stdin.readline()
    c = [[0 for _ in range(len(x)+2)] for _ in range(len(y)+2)] 
    print(lcm(c,len(x)-1,len(y)-1))