import sys
from collections import deque
def solve():
    try:
        line=sys.stdin.readline()
        if not line:
            return
        n=int(line.strip())
    except ValueError:
        return
    
    queue=deque()

    for _ in range(n):
        line=sys.stdin.readline().strip()
        if not line:
            continue
        parts=line.split()
        op_type=int(parts[0])

        if op_type==1:
            x=int(parts[1])
            queue.append(x)
        
        elif op_type==2:
            if queue:
                queue.popleft()
            else:
                print("ERR_CANNOT_POP")
        
        elif op_type==3:
            if queue:
                print(queue[0])
            else:
                print("ERR_CANNOT_QUERY")
        
        elif op_type==4:
            print(len(queue))

if __name__=="__main__":
    solve()