n = int(input())
ans = []
for _ in range(n):
    p = list(input().split())
    if p[0]=='1':#`1 x`:将整数 x ( − 1 0 9 ≦ x ≦ 1 0 9 ) x(−10 9 ≦x≦10 9 ) 入队;
        ans += [p[1]]
    elif p[0]=='2':#`2`:若队列非空,则仅将队头元素出队,否则输出 `ERR_CANNOT_POP`;
        if len(ans)!=0: ans.pop(0)
        else: print('ERR_CANNOT_POP')
    elif p[0]=='3':#`3`:查询并输出队首元素,队列为空时输出 `ERR_CANNOT_QUERY`;
        print(ans[0] if len(ans)!=0 else 'ERR_CANNOT_QUERY')
    elif p[0]=='4':#`4`:输出队列当前元素数量。
        print(len(ans))
    else:
        print(-1)