Given J's winners-bracket and losers-bracket win counts in a double-elimination tournament with 2^k players, find his final rank.
Medium7MathImplementationSimulationBinary searchNo attempts yetTime limit2sMemory limit512 MBYour friend J loves fighting games and plays them night and day. Every time you play with J, he wins by a crushing margin. You eventually suggest that J enter a fighting game tournament, where he can play people closer to his level. J decides to enter the Street League Pixel Championship, which is a bracketed double-elimination tournament.
A bracketed double-elimination tournament works as follows. There are two brackets: the winners bracket and the losers bracket. Everyone begins in the winners bracket. Each round, the players left in the winners bracket are paired and play matches. Winners stay in the winners bracket and losers drop down to the losers bracket.
Each round of the losers bracket is a minor stage followed by a major stage. Suppose that at the beginning of a round the losers bracket contains x players. In the minor stage, the x players in the losers bracket are paired and play matches. The x/2 winners stay in the losers bracket, and the other x/2 players are eliminated from the tournament. When x/2 players then drop down from the winners bracket, the major stage begins: the new pool of x players is paired and plays matches. The x/2 winners of this stage stay in the losers bracket, and the other x/2 players are eliminated from the tournament. This continues until each bracket holds a single player. These two players then play a match. If the player from the winners bracket wins, that player wins the tournament. Otherwise, the player from the winners bracket also drops down to the losers bracket, and the two play a final match (the grand finals). The winner of that match wins the tournament.

Since you are a good friend, you watch all of J's games. J wins w games in the winners bracket and ℓ games in the losers bracket. A win in the first match between the last player of each bracket counts toward the bracket the winner belongs to. The grand finals are played while both players are in the losers bracket, so a win there counts as a losers bracket win.
What is J's final rank in the tournament? Participants are ranked by when they are eliminated from the tournament. Everyone eliminated at the same time is tied, and they all receive the best rank they could share. The winner of the tournament is rank 1.
The first line contains a single integer T (1≤T≤10000), the number of test cases.
Each of the next T lines contains three integers k (0≤k≤30), w, and ℓ. They describe a valid double-elimination tournament with 2k competitors in which J wins w games in the winners bracket and ℓ games in the losers bracket.
For each tournament, print one line with a single integer: J's rank.
In the first tournament there are four competitors. J wins his first game in the winners bracket, after which there are two players in the winners bracket and two in the losers bracket. J loses his next game in the winners bracket and drops down to the losers bracket. There is now one player in the winners bracket and two in the losers bracket. J beats the other player in the losers bracket to reach the finals, where he defeats the winners bracket player twice to take the tournament.
In the second tournament there are four competitors. J wins two games in the winners bracket to reach the finals, where he wins to take the tournament.
In the third tournament there are eight competitors. Believe it or not, J is eliminated immediately, and so is one other player, so the two are tied for rank 7.