Help me in solving CAESAR problem

My issue

My code

t = int(input())
for i in range(t):
    n = int(input())
    s1 = input()
    s2 = input()
    s3 = input()
    diff = abs(ord(s1[0]) -ord(s2[0]))
    s4 = ''
    for i in s3:
        temp = (ord(i)+diff)
      
        if temp >=97 and temp<=122:
            s4 += chr(temp)
        else:
            temp = (temp%122) + 96
            s4 += chr(temp)
    print(s4)
            

Problem Link: CAESAR Problem - CodeChef

@bvrit_2840
You should check the editorial for the problem.

Here is how i solved it.

# cook your dish here
for _ in range(int(input())):
    l=""
    n=int(input())
    s=input()
    t=input()
    u=input()
    dif=ord(t[0])-ord(s[0])
    for i in u:
        val=ord(i)+dif
        if(val<97):
            val+=26
        elif(val>122):
            val-=26
        l=l+chr(val)
    print(l)