請求各位大佬幫我看一下
哪裡能夠改進
from sys import stdin
op = {'p':'q'}
for s in stdin:
n = int(s)
for x in range(n):
ans = 0
stack = []
sen = stdin.readline().replace('.','')
for k in sen:
if k in op:
stack.append(op[k])
elif stack:
if k == 'q':
g = stack.pop()
if g == k:
ans += 1
print(ans)