All problems

Problem 735

Design Twitter

Medium · 20 pts · Design, Heap, Hash Table

Problem

Simulate a small Twitter. 'post u t' makes user u post a tweet with id t (ids are unique). 'feed u' prints the ids of the 10 most recent tweets posted by u or by users u follows, newest first, or EMPTY if there are none. 'follow a b' makes a follow b, and 'unfollow a b' undoes it; following yourself or someone already followed changes nothing. Merging the followed users' tweet lists with a heap keyed by posting time gives each feed in O(F log F).

Input

The first line contains Q. Each of the next Q lines contains one operation. There is at least one feed operation.

Output

Print the result of each feed operation on its own line: tweet ids separated by spaces, or EMPTY.

Constraints

1 <= Q <= 30000; 1 <= user ids <= 500; 0 <= tweet ids <= 10^4; tweet ids are unique

Example input

7
post 1 5
feed 1
follow 1 2
post 2 6
feed 1
unfollow 1 2
feed 1

Example output

5
6 5
5

Explanation

User 1 posts tweet 5, so feed 1 shows 5. After 1 follows 2 and 2 posts 6, feed 1 shows 6 5, newest first. After 1 unfollows 2, feed 1 shows only 5 again.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Expression Add Operators Next: Fractional Knapsack