You are working for Metagonia stock exchange. Recently traders in Metagonia had heard about Iceberg orders traded on London stock exchange and asked your employer to add such functionality as well. A stock exchange is an engine that receives orders and generates trades.
An iceberg order is a quintuple of integers (ID, T, P, V, TV). Each order has an identifier (unique among all orders), type (which is equal to either BUY = 1 or SELL = 2), price , total remaining volume and tip volume . For each order, exchange additionally keeps track of its current volume and priority . There is also a global priority counter of the exchange . An order book of the exchange is a set of orders.
Trades that are generated by the exchange are quadruples of integers (BUY\text{-}ID, SELL\text{-}ID, P, V). Each trade has BUY\text{-}ID and SELL\text{-}ID - identifiers of matching BUY and SELL orders, trade price , and trade volume .
When an order is received by the exchange it is matched against orders currently on the order book. This is done as follows. Suppose an order is received with T_a = SELL. Among all orders currently on the order book we look for an order such that T_b = BUY and P_b \geq P_a. We select such an order with the largest price, and if there are several - one with the smallest priority. If there is such an order , then trade is generated with BUY\text{-}ID_t = ID_b and SELL\text{-}ID_t = ID_a at trade price P_t = P_b with trade volume V_t = \min(V_a,\,CV_b). V_a, V_b, and CV_b are all decreased by trade volume. If V_b = 0 after this, then the order is removed from the order book. If CV_b = 0 (but V_b > 0) then we set current volume of order to CV_b = \min(V_b,\,TV_b), set PR_b = GP, and increment . We continue these operations of selecting and generating trades until either V_a = 0 or there are no more orders on the order book which satisfy the condition. In the latter case, we add order to the order book with CV_a = \min(V_a,\,TV_a) and PR_a = GP, and then increment . When the process of matching the order is finished with several trades between the same pair of orders and (and there can be lots of them!), they are all united into a single trade with the volume equal to the sum of individual trade volumes.
If T_a = BUY we are looking for an order with T_b = SELL and P_b \leq P_a and select such an order with the smallest price and the smallest priority among those. The rest of the matching process is as described above, with trades having BUY\text{-}ID_t = ID_a, SELL\text{-}ID_t = ID_b, P_t = P_b, and V_t = \min(V_a,\,CV_b).
Initially the order book is empty. You are presented with several orders that are received by the exchange one by one. You need to print generated trades and the order book state after all orders are processed.
Hint: The priority and are introduced in the problem statement only for the purpose of a formal description of the algorithm. The actual implementation does not have to keep track of priority. Typically, an exchange simply keeps a priority-ordered list of orders of each type at each price in its order book.
The first line contains the number of orders (1 \leq n \leq 50000). Each of the following lines represent an order. Each order is given by a space-separated quintuple ID\,T\,P\,V\,TV, where 1 \leq ID \leq 1000000, T = 1 for BUY and T = 2 for SELL, 1 \leq P \leq 100000 and 1 \leq TV \leq V \leq 10^9.
For each order print all trades generated by processing this order, in ascending order of pairs (BUY\text{-}ID, SELL\text{-}ID), each trade on its own line. Each trade shall be printed as a space-separated quadruple of integers BUY\text{-}ID\,SELL\text{-}ID\,P\,V. It is guaranteed that the total number of trades would not exceed . Print a blank line after all trades, followed by the order book. Each order that is still on the book shall be printed as a space-separated sextuple ID\,T\,P\,V\,TV\,CV, sorted first by and then by .
Example 1
In the sample input the first four orders have T = BUY. Assuming that at the beginning at the exchange was equal to , after receiving these orders the order book looks in the following way when ordered according to the matching rules from the problem statement for T_b = BUY orders (first by the largest price, then by the smallest priority):

The fifth order (with ID_a = 4321) has T_a = SELL, P_a = 99 and V_a = 125 and is eligible for match with all of the above four orders given in the above table. First, it matches twice with the order with the highest price of for a total trade volume of , removes it from the order book, bumps to in the process, and decreases V_a to . Then, there are three other orders at price . One matching pass through them produces three trades for a total volume of (volume with order , volume with order , removes it from the book, volume with order ), bumps to , and decreases V_a to . The remaining orders in the book are shown below:

One last match with the order produces a trade with a volume of (for a total volume of for matches with order ) and the order is done (V_a = 0). Four corresponding total trades for order are printed in the sample output. The remaining order book is:

The sixth BUY order (with ID = 5678) is added to the order book ( becomes ):

The last, seventh order (with ID = 8765), can be matched only with the order due to price condition, generates a trade with volume of , order is removed from the order book, while order is added. Now the order book has both BUY and SELL orders:
