1398_E. Two Types of Spells
Polycarp plays a computer game (yet again). In this game, he fights monsters using magic spells.

There are two types of spells: fire spell of power x deals x damage to the monster, and lightning spell of power y deals y damage to the monster and doubles the damage of the next spell Polycarp casts. Each spell can be cast only once per battle, but Polycarp can cast them in any order.

For example, suppose that Polycarp knows three spells: a fire spell of power 5, a lightning spell of power 1, and a lightning spell of power 8. There are 6 ways to choose the order in which he casts the spells:

  * first, second, third. This order deals 5 + 1 + 2 ⋅ 8 = 22 damage; 
  * first, third, second. This order deals 5 + 8 + 2 ⋅ 1 = 15 damage; 
  * second, first, third. This order deals 1 + 2 ⋅ 5 + 8 = 19 damage; 
  * second, third, first. This order deals 1 + 2 ⋅ 8 + 2 ⋅ 5 = 27 damage; 
  * third, first, second. This order deals 8 + 2 ⋅ 5 + 1 = 19 damage; 
  * third, second, first. This order deals 8 + 2 ⋅ 1 + 2 ⋅ 5 = 20 damage. 



Initially, Polycarp knows 0 spells. His spell set changes n times, each time he either learns a new spell or forgets an already known one. After each change, calculate the maximum possible damage Polycarp may deal using the spells he knows.

Input

The first line contains one integer n (1 ≤ n ≤ 2 ⋅ 10^5) — the number of changes to the spell set.

Each of the next n lines contains two integers tp and d (0 ≤ tp_i ≤ 1; -10^9 ≤ d ≤ 10^9; d_i ≠ 0) — the description of the change. If tp_i if equal to 0, then Polycarp learns (or forgets) a fire spell, otherwise he learns (or forgets) a lightning spell.

If d_i > 0, then Polycarp learns a spell of power d_i. Otherwise, Polycarp forgets a spell with power -d_i, and it is guaranteed that he knew that spell before the change.

It is guaranteed that the powers of all spells Polycarp knows after each change are different (Polycarp never knows two spells with the same power).

Output

After each change, print the maximum damage Polycarp can deal with his current set of spells.

Example

Input


6
1 5
0 10
1 -5
0 5
1 11
0 -10


Output


5
25
10
15
36
21
```python
import bisect
import sys
input = sys.stdin.readline

class TreeSet(object):
    """
    Binary-tree set like java Treeset.
    Duplicate elements will not be added.
    When added new element, TreeSet will be sorted automatically.
    """
    def __init__(self, elements):
        self._treeset = []
        self.addAll(elements)

    def addAll(self, elements):
        for element in elements:
            if element in self: continue
            self.add(element)

    def add(self, element):
        if element not in self:
            bisect.insort(self._treeset, element)

    def ceiling(self, e):
        index = bisect.bisect_right(self._treeset, e)
        if self[index - 1] == e:
            return e
        return self._treeset[bisect.bisect_right(self._treeset, e)]

    def floor(self, e):
        index = bisect.bisect_left(self._treeset, e)
        if self[index] == e:
            return e
        else:
            return self._treeset[bisect.bisect_left(self._treeset, e) - 1]

    def __getitem__(self, num):
        return self._treeset[num]

    def __len__(self):
        return len(self._treeset)

    def clear(self):
        """
        Delete all elements in TreeSet.
        """
        self._treeset = []

    def clone(self):
        """
        Return shallow copy of self.
        """
        return TreeSet(self._treeset)

    def remove(self, element):
        """
        Remove element if element in TreeSet.
        """
        try:
            self._treeset.remove(element)
        except ValueError:
            return False
        return True

    def __iter__(self):
        """
        Do ascending iteration for TreeSet
        """
        for element in self._treeset:
            yield element

    def pop(self, index):
        return self._treeset.pop(index)

    def __str__(self):
        return str(self._treeset)

    def __eq__(self, target):
        if isinstance(target, TreeSet):
            return self._treeset == target.treeset
        elif isinstance(target, list):
            return self._treeset == target

    def __contains__(self, e):
        """
        Fast attribution judgment by bisect
        """
        try:
            return e == self._treeset[bisect.bisect_left(self._treeset, e)]
        except:
            return False


n = int(input())
MAX_VALUE = int(1e9+1)
ts = TreeSet([-MAX_VALUE, MAX_VALUE])
info = {}
cntL = 0
cntL2 = 0
min2 = MAX_VALUE
sum1 = 0
sum2 = 0
for _ in range(n):
    type, power = list(map(int, input().split()))
    if power>0:
        ts.add(power)
        info[power] = type
        if min2 == ts[len(ts) - cntL-1]:
            sum1 += power
            if type == 1:
                cntL += 1
                min2 = ts[len(ts) - cntL - 1]
                sum2 += min2
                sum1 -= min2
                cntL2 += info[min2]
        else:
            sum2 += power
            cntL2 += info[power]
            if type == 0:
                sum2 -= min2
                sum1 += min2
                cntL2 -= info.get(min2, 0)
                # min2 = ts[len(ts) - cntL - 1]
            else:
                cntL += 1
    else:
        power = abs(power)
        ts.remove(power)
        if min2 == ts[len(ts) - cntL - 1]:
            sum1 -= power
            if type == 1:
                sum2 -= min2
                sum1 += min2
                cntL2 -= info.get(min2, 0)
                cntL -= 1
                # min2 = ts[len(ts) - cntL - 1]
        else:
            sum2 -= power
            cntL2 -= info[power]
            if type == 0:
                min2 = ts[len(ts) - cntL - 1]
                sum2 += min2
                sum1 -= min2
                cntL2 += info[min2]
            else:
                cntL -= 1
    min2 = ts[len(ts) - cntL - 1]
    if cntL2 < cntL or cntL == 0:
        ans = 2*sum2 + sum1
    else:
        if len(ts) - cntL - 2 > 0:
            max1 = ts[len(ts) - cntL - 2]
        else:
            max1 = 0
        ans = 2*sum2 - min2 + sum1 + max1
    print(ans)

```