Universal Cup Judging System

Universal Cup

Time Limit: 2.0 s Memory Limit: 512 MB Total points: 100 Hackable ✓
Statistics

Jelefy is playing the Tower of Hanoi with a special rule.

The Tower of Hanoi has three pillars, $A$, $B$, and $C$, on which disks can be stacked. Disks can be moved between different pillars, but must adhere to the following rules:

  • Only one disk can be moved at a time;
  • Each move can only move the top disk of one pillar to the top of another pillar;
  • On pillars $B$ and $C$, no disk can be placed on top of a smaller disk;
  • On pillar $A$, larger disks can be placed on top of smaller disks.

At the beginning, Jelefy randomly stacks $n$ disks of different sizes on pillar $A$ and leaves pillars $B$ and $C$ empty. Under the above conditions, Jelefy wants to know how many moves are needed to move all the disks from pillar $A$ to pillar $B$.

Input

The first line contains an integer $T$ ($1\leq T\leq 10^4$), indicating the number of test cases.

For each test case: The first line contains an integer $n$ ($1\leq n\leq 10^5$), representing the number of disks on pillar $A$ in the initial state.

The second line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\leq p_i\leq n$), where $p_i$ represents the diameter of the $i$-th disk on pillar $A$ from bottom to top. It is guaranteed that $p_1,p_2,\ldots,p_n$ form a permutation of length $n$, i.e., each integer from $1$ to $n$ appears exactly once in the sequence.

For all test cases, it is guaranteed that $\sum n\leq 2\times 10^5$.

Output

For each test case, output an integer on a single line, representing the minimum number of moves required.

Example

Input

3
5
1 5 3 2 4
5
1 2 3 4 5
6
5 3 6 1 4 2

Output

11
5
19

Explanation

In the first example, one possible solution with the minimum number of moves is as follows ($X\rightarrow Y$ indicates moving the top disk from pillar $X$ to the top of pillar $Y$):

  • $A\rightarrow C$
  • $A\rightarrow B$
  • $A\rightarrow C$
  • $B\rightarrow C$
  • $A\rightarrow B$
  • $C\rightarrow A$
  • $C\rightarrow A$
  • $C\rightarrow B$
  • $A\rightarrow B$
  • $A\rightarrow B$
  • $A\rightarrow B$

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.