You are walking at midnight along a road that has checkpoints numbered from to from left to right. Each checkpoint may be empty, or it may have angels or demons stationed there. If a checkpoint has an angel or demon, they will cast a blessing or curse of power upon you which will be active till checkpoint . This blessing or curse will be in effect at all the checkpoints for . If you receive a blessing of power , your health will increase by at each checkpoint where it is active. Conversely, if you receive a curse of power , your health will decrease by at each checkpoint where it is active.
Formally, if an angel with power is stationed at checkpoint and has an endpoint at checkpoint , then for each checkpoint
If at any checkpoint (including checkpoint ) your health becomes zero or negative, you will die. What is the minimum health you need to start with, to survive the walk from checkpoint to ? Please note here, the initial health must always be an integer value.
Note that there may be multiple angels and/or demons stationed at some checkpoints, and angels are slightly more powerful than demons. So if both angels and demons are active at a checkpoint, then all the blessings will take effect first, followed by the curses. Please refer to the explanation of the sample for better understanding.
The first line of the input will contain an integer,
Each of the test cases starts with two integers,
If then it is an angel, otherwise it is a demon.
It is guaranteed that the sum of over all test cases .
For each test case, output a single integer, the minimum amount of health needed to survive the walk.
1 10 3 2 1 3 2 1 1 2 2 1 2 3 1
Explanation of the sample:
You start with health 1. It can be shown that you cannot start with any integer health value lesser than this and be alive while completing the journey.
At checkpoint 1 first you get a blessing of power 2, your health becomes 3, then you get a curse of power 2 and your health drops to 1.
At checkpoint 2 first you get two blessings of power 1 and 2, your health becomes 1 + 1 + 2 = 4, then you get a curse of power 2, your health becomes 2.
At checkpoint 3, first you get a blessing of power 1, your health becomes 3, then you get a curse of power 2, your health becomes 1.
Then you continue with health 1 to checkpoint 10 to finish your journey. You didn’t die and you made it to the end! Congrats!