Problem: 2050E - Three Strings
Language: python

Solution 1 (runtime: 1374 ms):
FILE = False


class Solution:
    def __init__(self) -> None:
        pass

    def solve(self, a: str, b: str, c: str) -> int:
        dp = [[0 for _ in range(len(b) + 1)] for _ in range(len(a) + 1)]

        top_row = dp[0]
        for i_b in range(1, len(b) + 1):
            i_c = i_b
            top_row[i_b] = top_row[i_b - 1] + (1 if c[i_c - 1] != b[i_b - 1] else 0)

        for i_a in range(1, len(a) + 1):
            i_c = i_a
            dp[i_a][0] = dp[i_a - 1][0] + (1 if c[i_c - 1] != a[i_a - 1] else 0)

        for i_a in range(1, len(a) + 1):
            for i_b in range(1, len(b) + 1):
                i_c = i_a + i_b
                dp[i_a][i_b] = min(
                    dp[i_a - 1][i_b] + (1 if c[i_c - 1] != a[i_a - 1] else 0),
                    dp[i_a][i_b - 1] + (1 if c[i_c - 1] != b[i_b - 1] else 0),
                )

        return dp[-1][-1]


if __name__ == "__main__":
    if FILE:
        FILE_NAME = "CF_991_div3/inputs/e.txt"
        lines = iter(open(FILE_NAME, "r").read().splitlines())
        get_input = lambda: next(lines)
    else:
        get_input = input

    n_test_cases = int(get_input())
    for _ in range(n_test_cases):
        solution = Solution()
        a, b, c = get_input(), get_input(), get_input()
        print(solution.solve(a, b, c))


Solution 2 (runtime: 280 ms):
# import sys
# sys.setrecursionlimit(2*(10**3))
# for _ in range(int(input())):
#     a = input()
#     b = input()
#     c = input()
#     dic = {}

#     def calculate(i,j,k,dic):
#         if (i,j,k) in dic:
#             return dic[(i,j,k)]
#         if i == len(a):
#             count = 0
#             while k <len(c):
#                 if c[k] != b[j]:
#                     count+=1
#                 k+=1
#                 j+=1
#             dic[(i,j,k)] = count
#             return count
#         if j == len(b):
#             count = 0
#             while k < len(c):
#                 if c[k] != a[i]:
#                     count+=1
#                 i+=1
#                 k+=1
#             dic[(i,j,k)] = count
#             return count 

#         if c[k]!=a[i] and c[k]!=b[j]:
#             count = 1 + min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))
#         elif c[k]!=a[i] and c[k] == b[j]:
#             count = min(calculate(i,j+1,k+1,dic),calculate(i+1,j,k+1,dic)+1)
#         elif c[k]==a[i] and c[k] != b[j]:
#             count = min(calculate(i,j+1,k+1,dic)+1,calculate(i+1,j,k+1,dic))
#         else:
#             count = min(calculate(i+1,j,k+1,dic),calculate(i,j+1,k+1,dic))

#         dic[(i,j,k)] = count
#         return count    
            
#     print(calculate(0,0,0,dic))

for _ in range(int(input())):
    a = input()
    b = input()
    c = input()

    dp = [[float('inf')] * (len(b) + 1) for _ in range(len(a) + 1)]

    for i in range(len(a) + 1):
        for j in range(len(b) + 1):
            k = i + j
            if k > len(c):
                continue

            if i == 0 and j == 0:
                dp[i][j] = 0
            else:
                if i > 0 and c[k - 1] == a[i - 1]:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j])
                if j > 0 and c[k - 1] == b[j - 1]:
                    dp[i][j] = min(dp[i][j], dp[i][j - 1])
                if i > 0 and c[k - 1] != a[i - 1]:
                    dp[i][j] = min(dp[i][j], dp[i - 1][j] + 1)
                if j > 0 and c[k - 1] != b[j - 1]:
                    dp[i][j] = min(dp[i][j], dp[i][j - 1] + 1)

    print(dp[len(a)][len(b)])