Problem:
Doubly Linked List

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list.

deleteLast: delete the last element from the list.

Input

The input is given in the following format:

n

command1

command2

...

commandn

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst

deleteLast

Output

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

Solution in Python3 (judge verdict: Memory Limit Exceeded)

#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_3_C&lang=jp
#???????????£????????????
#??¬?????????????????¬????????§????????¨???????????????????????????dummy????????????????????¨??§???????????????????????????????????§??????
def dll_processor(operations):
    
    front = None
    end = None
    
    for o in operations:
        if o[0] == "insert":
            front, end = insert(front, end,{ "next":None, "prev":None, "data":int(o[1]) })
        elif o[0] == "delete":
            front, end = delete(front, end, int(o[1]))
        elif o[0] == "deleteFirst":
            front, end = delete_first(front, end)
        elif o[0] == "deleteLast":
            front, end = delete_last(front, end)
        #print(get_list(front))
        
    return get_list(front)

def get_list(front):
    if not front:
        return []
    
    l = []
    target = front
    while True:
        l.append(target["data"])
        if not target["next"]:
            break
        target = target["next"]
    return l

def insert(front, end, target):

    if front:
        front["prev"] = target
        target["next"] = front
        return target, end
    else:
        return target, target
    
def delete(front, end,target):

    delete_node = front
    while not delete_node["data"] == target:
        delete_node = delete_node["next"]
        if delete_node == None:
            return front, end

    if delete_node["prev"] == None:
        delete_node["next"]["prev"] = None
        return delete_node["next"], end
    elif delete_node["next"] == None:
        delete_node["prev"]["next"] = None
        return front, delete_node["prev"]
    else:
        delete_node["next"]["prev"] = delete_node["prev"]
        delete_node["prev"]["next"] = delete_node["next"]
        return front, end
    
def delete_last(front, end):
    
    if not end["prev"]:
        return None, None
    else:
        end["prev"]["next"] = None
        return front, end["prev"]
    
def delete_first(front, end):
    
    if not front["next"]:
        return None, None
    else:
        front["next"]["prev"] = None
        return front["next"], end

def main():
    n_list = int(input())
    target_list = [input().split() for i in range(n_list)]
    print(*dll_processor(target_list))
    
if __name__ == "__main__":
    main()