Problem: 2050E - Three Strings
Language: PyPy 3-64

Previous submission (verdict: Memory limit exceeded on test 16):
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))

Submission (verdict: Accepted):
# 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)])