Go: package main

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

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

func scanString() string {
	sc.Scan()
	return sc.Text()
}

func init() {
	sc.Split(bufio.ScanWords)
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func lcs(a, b string) int {
	lenA := len(a)
	lenB := len(b)
	dp := make([][]int, lenA+1)
	for i := range dp {
		dp[i] = make([]int, lenB+1)
	}

	for i := 1; i <= lenA; i++ {
		for j := 1; j <= lenB; j++ {
			if a[i-1] == b[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[lenA][lenB]
}

func main() {
	n := scanInt()
	w := bufio.NewWriter(os.Stdout)
	for i := 0; i < n; i++ {
		a := scanString()
		b := scanString()
		fmt.Fprintln(w, lcs(a, b))
	}
	w.Flush()
}

Python: n = int(input())
import sys

def input():
    return sys.stdin.readline().rstrip()
ans = []
def calc(s1,s2):
    lens1 = len(s1)
    lens2 = len(s2)
    # print(len(s1),s1[-1])
    dp = [0]*(lens2+1)
    for i in range(lens1):
        dp2 = dp[:]
        e = s1[i]
        for j in range(lens2):
            if e == s2[j]:
                dp[j+1] = dp2[j] + 1
            elif dp[j]>dp[j+1]:
                dp[j+1] = dp[j]
    # print(dp)
    ans.append(dp[-1])


for i in range(n):
    s1 = input()
    s2 = input()
    calc(s1,s2)
for i in ans:
    print(i)

# 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())
# ret = []
# for i in range(n):
#     s1 = input().rstrip()
#     s2 = input().rstrip()
#     ret.append(lcs(s1, s2))
# print(*ret, sep="\n")

Python: # -*- coding: utf-8 -*-
import sys
from itertools import product
sys.setrecursionlimit(100000)

N = int(input())

def calc_LCS(n, m):
    if n == 0 or m == 0:
        return 0
    elif X[n] == Y[m]:
        LCS[n][m] = LCS[n-1][m-1] + 1
        return LCS[n][m]
    else:
        LCS[n][m] = max(LCS[n-1][m], LCS[n][m-1])
        return LCS[n][m]
        

for _ in range(N):
    X = ["  "] + list(input())
    Y = [" "] + list(input())
    LCS = [[0 for _ in range(len(Y))] for _ in range(len(X))]
    max_ = 0
    for n, m in product(range(len(X)), range(len(Y))):
        max_ = max(max_, calc_LCS(n, m))
    print(max_)

Python: from collections import defaultdict


def longest_common_subsequence(s, t):
    dp = [[0] * (len(t) + 1) for _ 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 + 1][j], dp[i][j + 1])
    return dp[-1][-1]


def longest_common_subsequence2(s, t):
    dp = [0] * (len(t) + 1)
    for i in range(len(s)):
        si = s[i]
        tmp = dp[:]
        for j in range(len(t)):
            if si == t[j]:
                dp[j + 1] = tmp[j] + 1
            elif dp[j + 1] < dp[j]:
                dp[j + 1] = dp[j]
    return dp[-1]


def main():
    n = int(input())
    for _ in range(n):
        X, Y = input(), input()
        print(longest_common_subsequence2(X, Y))


if __name__ == '__main__':
    main()

Python: def lcs(str1,str2):
    c=[[0 for _ in range(100000)] for _ in range(100000)]
    m=len(str1)
    n=len(str2)
    maxl=0
    str1=" "+str1
    str2=" "+str2

    i=1
    j=1
    while(i<=m):
        while(j<=n):
            if(str1[i]==str2[j]):
                c[i][j]=c[i-1][j-1]+1
              #  print(c[i][j])
            else:
                c[i][j]=max(c[i-1][j],c[i][j-1])
            maxl=max(maxl,c[i][j])
            j+=1
        i+=1
        j=0

    return maxl

def main():
    n=int(input())
    i=0
    while(i<n):
        str1=str(input())
        str2=str(input())
        print(int(lcs(str1,str2)))
        i+=1

main()

Python: def lcs(x, y):
    a = len(x)
    b = len(y)
    c1 = [0] * (b + 1)
    for i in range(a):
        c2 = c1[:]
        for j in range(b):
            if x[i] == 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())
ret = [0] * n
for i in range(n):
    s1 = input().rstrip()
    s2 = input().rstrip()
    ret[i] = lcs(s1, s2)
for i in range(n):
    print(ret[i])

Python: q = int(input())
for i in range(q):
    x = input()
    y = input()
    co = []

    for j, s in enumerate(x):
        co.append([])
        co[j].append(-1)
        if j == 0:
            end = y.find(s)
            if end != -1:
                co[j].append(end)
        else:
            for k, t in enumerate(co[j-1][:-1]):
                end = y.find(s, t+1)
                if end != -1:
                    co[j].append(min(end, co[j-1][k+1]))
                else:
                    co[j].append(co[j-1][k+1])
            end = y.find(s, co[j-1][-1]+1)

            if end != -1:
                co[j].append(end)

    print(len(co[-1])-1)

Python: #!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
input:
3
abcbdab
bdcaba
abc
abc
abc
bc

output:
4
3
2

"""
import sys


def lcs(A, B):
    m, n = len(A), len(B)
    count_list = tuple([[0] * (n + 1) for _ in range(m + 1)])

    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if A[i - 1] == B[j - 1]:
                count_list[i][j] = count_list[i - 1][j - 1] + 1
            else:
                count_list[i][j] = max(count_list[i - 1][j], count_list[i][j - 1])

    return count_list[m][n]


def lcs_better(A, B):
    cost_history = [None]
    for ele in B:
        for index in range(len(cost_history) - 1, -1, -1):
            # ???????¬??????°????????????????§???¨A??\?????°??????(ele), ????????°?????????
            found = A.find(ele, cost_history[index])
            if found != -1:
                if index + 1 < len(cost_history):
                    cost_history[index + 1] = min(cost_history[index + 1], found + 1)
                # ??¨???????????°?????¢?????°?????¶?????°????????´???
                else:
                    # assert index + 1 == len(cost_history)
                    cost_history.append(found + 1)

    return len(cost_history) - 1


if __name__ == '__main__':
    _input = sys.stdin.readlines()
    pair_num = int(_input[0])

    word_list = _input[1:]
    # assert len(word_list) == 2 * pair_num
    for k in range(0, len(word_list), 2):
        print(lcs(word_list[k].strip(), word_list[k + 1].strip()))

Python: import sys
def lcs(X, Y):
    x = len(X) + 1
    y = len(Y) + 1
    c = [0 for y in range(y)]

    for i in range(1, x):
        tmp = c[:]
        for j in range(1, y):
            if X[i - 1] == Y[j - 1]:
                c[j] = tmp[j - 1] + 1
            else:
                c[j] = max(tmp[j], c[j - 1])
    return c[y - 1]

n = int(input())

for i in range(n):
    X = sys.stdin.readline()[:-1]
    Y = sys.stdin.readline()[:-1]
    print(lcs(X, Y))

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 t<1: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))