from sys import stdin
deque = []
for _ in range(int(stdin.readline())):
    arr = stdin.readline().split()
    if arr[0] == 'push_front':
        deque.insert(0, arr[1])
    if arr[0] == 'push_back':
        deque.append(arr[1])
    elif arr[0] == 'pop_front':
        if deque:
            print(deque.pop(0))
        else:
            print(-1)
    elif arr[0] == 'pop_back':
        if deque:
            print(deque.pop(-1))
        else:
            print(-1)
    elif arr[0] == 'size':
        print(len(deque))
    elif arr[0] == 'empty':
        print(1 - int(bool(deque)))
    elif arr[0] == 'front':
        if deque:
            print(deque[0])
        else:
            print(-1)
    elif arr[0] == 'back':
        if deque:
            print(deque[-1])
        else:
            print(-1)
    else:
        pass

d69c6278c31ceae9185747cead3468cfffbe74b6
<mask token>

<mask token>
for _ in range(int(stdin.readline())):
    arr = stdin.readline().split()
    if arr[0] == 'push_front':
        deque.insert(0, arr[1])
    if arr[0] == 'push_back':
        deque.append(arr[1])
    elif arr[0] == 'pop_front':
        if deque:
            print(deque.pop(0))
        else:
            print(-1)
    elif arr[0] == 'pop_back':
        if deque:
            print(deque.pop(-1))
        else:
            print(-1)
    elif arr[0] == 'size':
        print(len(deque))
    elif arr[0] == 'empty':
        print(1 - int(bool(deque)))
    elif arr[0] == 'front':
        if deque:
            print(deque[0])
        else:
            print(-1)
    elif arr[0] == 'back':
        if deque:
            print(deque[-1])
        else:
            print(-1)
    else:
        pass

<mask token>
deque = []
for _ in range(int(stdin.readline())):
    arr = stdin.readline().split()
    if arr[0] == 'push_front':
        deque.insert(0, arr[1])
    if arr[0] == 'push_back':
        deque.append(arr[1])
    elif arr[0] == 'pop_front':
        if deque:
            print(deque.pop(0))
        else:
            print(-1)
    elif arr[0] == 'pop_back':
        if deque:
            print(deque.pop(-1))
        else:
            print(-1)
    elif arr[0] == 'size':
        print(len(deque))
    elif arr[0] == 'empty':
        print(1 - int(bool(deque)))
    elif arr[0] == 'front':
        if deque:
            print(deque[0])
        else:
            print(-1)
    elif arr[0] == 'back':
        if deque:
            print(deque[-1])
        else:
            print(-1)
    else:
        pass

from sys import stdin
deque = []
for _ in range(int(stdin.readline())):
    arr = stdin.readline().split()
    if arr[0] == 'push_front':
        deque.insert(0, arr[1])
    if arr[0] == 'push_back':
        deque.append(arr[1])
    elif arr[0] == 'pop_front':
        if deque:
            print(deque.pop(0))
        else:
            print(-1)
    elif arr[0] == 'pop_back':
        if deque:
            print(deque.pop(-1))
        else:
            print(-1)
    elif arr[0] == 'size':
        print(len(deque))
    elif arr[0] == 'empty':
        print(1 - int(bool(deque)))
    elif arr[0] == 'front':
        if deque:
            print(deque[0])
        else:
            print(-1)
    elif arr[0] == 'back':
        if deque:
            print(deque[-1])
        else:
            print(-1)
    else:
        pass
