## RMID2 - Running Median Again

Danish has just solved the problem Running Median.

The first line of the problem says "You will be given some integers in non-decreasing order". The problem asks you to report and remove the median of the list every time it is queried.

Having solved this problem, Danish now begins to wonder how to solve the problem if the input is in any order (not necessarily non-decreasing order as mentioned above).

Can you help him?

Your task is to take as input a list of positive integers. Whenever -1 is given as input, you must output the median of the list, and remove it from the list. Take the smaller element as the median in case of even number of elements.

### Input

The input contains several test caes.

The first line contains an integer t, the number of test cases.

Each test case has several lines, each containing an integer n (<=10^9) . If n is positive, add it to the list. n=-1 indicates a median query (there will be no median query if the list is empty). The test case is terminated by n=0.

In each test case, there will be upto 10^5 integers to be added to the list, and upto 10^5 median queries.

### Output

For each median query as described above, print the median on a new line.

### Example

```Input:
1
9
10
2
5
1
18
-1
-1
4
3
-1
8
7
-1
0

Output:
5
9
3
7```

## Code Examples

### #1 Code Example with C++ Programming

```Code - C++ Programming```

``````#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define MOD                 1000000007LL
#define EPS                 1e-9
#define io                  ios_base::sync_with_stdio(false);cin.tie(NULL);

int main(){
io;
// if even numbers then lower one is median
ll n;
priority_queue<ll, vector<ll>, less < ll> > lowers;	// max pq
priority_queue<ll, vector<ll>, greater<ll> > uppers;	// min pq
vector<ll> ans;
int t;
cin >> t;
while(t--){
while(cin >> n){
if(n == 0){
for(ll i = 0; i < ans.size(); i++)
cout << ans[i] << endl;
cout << endl;
ans.clear();
while(!lowers.empty())	lowers.pop();
while(!uppers.empty())	uppers.pop();
break;
}
else if(n == -1){
ll median = lowers.top();
ans.push_back(median);
lowers.pop();
if(lowers.size() < uppers.size()){
lowers.push(uppers.top());
uppers.pop();
}
}else{
ll curr = n;
if(lowers.size() == 0 && uppers.size() == 0){
lowers.push(curr);
continue;
}
if(uppers.size() < lowers.size()){
if(curr < lowers.top()){
// difference more than 1
lowers.push(curr);
uppers.push(lowers.top());
lowers.pop();
}else
uppers.push(curr);
}else{
// 	uppers size greater or equal
if(curr > uppers.top()){
uppers.push(curr);
lowers.push(uppers.top());
uppers.pop();
}else
lowers.push(curr);
}
}
}

}
return 0;
}``````
Copy The Code &

Input

cmd
1
9
10
2
5
1
18
-1
-1
4
3
-1
8
7
-1
0

Output

cmd
5
9
3
7