• + 0 comments

    Really using two stacks:

    n=int(input())
    stack_in=[]
    stack_out=[]
    
    def transfer():
        while stack_in:
            stack_out.append(stack_in.pop())
    
    for _ in range(n):
        arr=input().split(' ')
        if arr[0]=='1':
            stack_in.append(int(arr[1]))        
        elif arr[0]=='2':
            if not stack_out: 
                transfer()
            if stack_out: 
                stack_out.pop()
        else:
            if not stack_out: 
                transfer()
            if stack_out: 
                print(stack_out[-1])