Runtime Error(NZEC) in Infix to Postfix

Can anybody tell me what’s wrong with my code? I have been trying this problem for weeks but still no result. I am getting correct ouput for all testcases and my custom testcases but when I submit it shows NZEC runtime error. Help me please!!!

    # cook your dish here
def prec(op):
    if op=='*' or op=='/':
        return 2
    elif op=='+' or op=='-':
        return 1
    elif op=='^':
        return 3
t=int(input())
while t>0:
    n=int(input())
    s=input()
    st=[]
    for ch in s:
        if ch.isalpha():
            print(ch,end='')
        elif ch==')':
            while len(st)!=0:
                if st[-1]=='(':
                    st.pop()
                    break
                print(st.pop(),end='')
        elif ch=='(' or len(st)==0 or st[-1]=='(' or prec(st[-1])<prec(ch):
            st.append(ch)
        else:
            while len(st)!=0:
                if st[-1]=='(':
                    break
                elif prec(st[-1])>=prec(ch):
                    print(st.pop(),end='')
                else:
                    break
            st.append(ch)
    while len(st)!=0:
        print(st.pop(),end='')
    print()
    t=t-1

use try and except blocks.