def LCS(s,t,restore=False):

    dp = [[0 for j in range(len(t)+1)] for i 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])

    if restore:

        lcs = []

        stack = [(len(s),len(t))]

        while stack:

            x,y = stack.pop()

            if dp[x][y] == 0:

                break

            if x-1 >= 0 and dp[x][y] == dp[x-1][y]:

                stack.append((x-1,y))

            elif y-1 >= 0 and dp[x][y] == dp[x][y-1]:

                stack.append((x,y-1))

            else:

                lcs.append(s[x-1])

                stack.append((x-1,y-1))

        lcs = ''.join(lcs)[::-1]

        return dp[-1][-1],lcs

    return dp[-1][-1]

  

S = eval(input())

T = eval(input())



print((LCS(S,T,True)[1]))
S = eval(input())

T = eval(input())



dp = [[0 for j in range(len(T) + 1)] for i 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] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1])

        else:

            dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])



res = []

stack = [(len(S), len(T))]



while stack:

    x, y = stack.pop()

    if dp[x][y] == 0:

        break

    if x - 1 >= 0 and dp[x][y] == dp[x - 1][y]:

        stack.append((x - 1, y))

    elif y - 1 >= 0 and dp[x][y] == dp[x][y - 1]:

        stack.append((x, y - 1))

    else:

        res.append(S[x - 1])

        stack.append((x - 1, y - 1))



print((''.join(res[::-1])))
def LCS(s, t, restore=False):
    dp = [[0 for j in range(len(t) + 1)] for i 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])
    if restore:
        lcs = []
        stack = [(len(s), len(t))]
        while stack:
            x, y = stack.pop()
            if dp[x][y] == 0:
                break
            if x - 1 >= 0 and dp[x][y] == dp[x - 1][y]:
                stack.append((x - 1, y))
            elif y - 1 >= 0 and dp[x][y] == dp[x][y - 1]:
                stack.append((x, y - 1))
            else:
                lcs.append(s[x - 1])
                stack.append((x - 1, y - 1))
        lcs = "".join(lcs)[::-1]
        return dp[-1][-1], lcs
    return dp[-1][-1]


S = eval(input())
T = eval(input())
print((LCS(S, T, True)[1]))

S = eval(input())
T = eval(input())
dp = [[0 for j in range(len(T) + 1)] for i 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] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1])
        else:
            dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1])
res = []
stack = [(len(S), len(T))]
while stack:
    x, y = stack.pop()
    if dp[x][y] == 0:
        break
    if x - 1 >= 0 and dp[x][y] == dp[x - 1][y]:
        stack.append((x - 1, y))
    elif y - 1 >= 0 and dp[x][y] == dp[x][y - 1]:
        stack.append((x, y - 1))
    else:
        res.append(S[x - 1])
        stack.append((x - 1, y - 1))
print(("".join(res[::-1])))

-def LCS(s, t, restore=False): -    dp = [[0 for j in range(len(t) + 1)] for i 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]) -    if restore: -        lcs = [] -        stack = [(len(s), len(t))] -        while stack: -            x, y = stack.pop() -            if dp[x][y] == 0: -                break -            if x - 1 >= 0 and dp[x][y] == dp[x - 1][y]: -                stack.append((x - 1, y)) -            elif y - 1 >= 0 and dp[x][y] == dp[x][y - 1]: -                stack.append((x, y - 1)) -            else: -                lcs.append(s[x - 1]) -                stack.append((x - 1, y - 1)) -        lcs = "".join(lcs)[::-1] -        return dp[-1][-1], lcs -    return dp[-1][-1] - - -print((LCS(S, T, True)[1])) +dp = [[0 for j in range(len(T) + 1)] for i 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] = max(dp[i][j] + 1, dp[i + 1][j], dp[i][j + 1]) +        else: +            dp[i + 1][j + 1] = max(dp[i + 1][j], dp[i][j + 1]) +res = [] +stack = [(len(S), len(T))] +while stack: +    x, y = stack.pop() +    if dp[x][y] == 0: +        break +    if x - 1 >= 0 and dp[x][y] == dp[x - 1][y]: +        stack.append((x - 1, y)) +    elif y - 1 >= 0 and dp[x][y] == dp[x][y - 1]: +        stack.append((x, y - 1)) +    else: +        res.append(S[x - 1]) +        stack.append((x - 1, y - 1)) +print(("".join(res[::-1])))