Problem: 2050E - Three Strings
Language: python

Solution 1 (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)])


Solution 2 (runtime: 390 ms):
def min_changes(a, b, c):

    n, m = len(a), len(b)

    

    # DP array

    dp = [[float('inf')] * (m + 1) for _ in range(n + 1)]

    dp[0][0] = 0

    

    for i in range(n + 1):

        for j in range(m + 1):

            # Skip invalid states

            if dp[i][j] == float('inf'):

                continue

            

            k = i + j  # Position in c

            

            # We can take the next character from a

            if i < n:

                cost = 0 if a[i] == c[k] else 1

                dp[i+1][j] = min(dp[i+1][j], dp[i][j] + cost)

            

            # We can take the next character from b

            if j < m:

                cost = 0 if b[j] == c[k] else 1

                dp[i][j+1] = min(dp[i][j+1], dp[i][j] + cost)

    

    return dp[n][m]



def main():

    t = int(input())

    

    for _ in range(t):

        a = input().strip()

        b = input().strip()

        c = input().strip()

        

        print(min_changes(a, b, c))



if __name__ == "__main__":

    main()