Infix to Postfix LRNDSA02

Someone please help me with my python code for https://www.codechef.com/LRNDSA02/problems/INPSTFIX

precedence = {'+':1, '-':1, '*':2, '/':2, '^':3}
def preced(i):
    if i in precedence and stack[-1] in precedence and precedence[i]<=precedence[stack[-1]]:
        return True
    else:
        return False
for _ in range(int(input())):
    n = int(input())
    s = input()
    out = []
    stack = []
    cnt = 0
    for i in s:
        if i.isalpha():
            out.append(i)
        elif i == '(':
            stack.append(i)
            cnt += 1
        elif i == ')':
            while cnt and stack[-1] != '(':
                out.append(stack.pop())
                cnt -= 1
            if not cnt and stack[-1] != '(':
                print("improper sequence")
            else:
                stack.pop()
                cnt -= 1
        else:
            while cnt and preced(i):
                out.append(stack.pop())
                cnt -= 1
            stack.append(i)
            cnt += 1
    while cnt:
        out.append(stack.pop())
        cnt -= 1
    print(''.join(out))

I am getting the output for all the different test-cases I checked. But for the above code I am getting WA. https://www.codechef.com/viewsolution/32188513

Please help me where/ In which test-case my program is failing…
Thanks in advance.

If anyone curious:
The problem was in taking the input without striping the end-line.
(As whenever the char is ‘\n’ the code is evaluating to else condition considering it as ‘operator’)

  • wrong code snippet : s = input()
  • correct code snippet: s = input().rstrip()

Thanks.