Under construction

Still being built. Things may change

TSP

Time limit4sMemory limit1024 MB

Summary
Given K vertices in an implicit binary heap-shaped tree with up to 10^18 nodes, find the shortest walk that visits all of them.
Level

Medium6 of 10

Topics
Tree, Sorting, DFS
Solved
No attempts yet

Problem

The tree has 101810^{18} vertices. For every integer ii with 2≤i≤10182 \le i \le 10^{18}, there is an edge between vertex ii and vertex ⌊i2⌋\left\lfloor \frac{i}{2} \right\rfloor. You are given KK vertices v1,v2,⋯ ,vKv_1, v_2, \cdots, v_K in this tree. Find the length of the shortest path that starts at any vertex and visits each of the given KK vertices at least once.

Input

The first line contains the number of test cases TT. (1≤T≤10 000)(1 \le T \le 10\,000)

The first line of each test case contains the number of vertices KK. (2≤K≤100 000)(2 \le K \le 100\,000)

The second line of each test case contains the vertex numbers v1,v2,⋯ ,vKv_1, v_2, \cdots, v_K, separated by spaces. All viv_i are distinct. (1≤vi≤1018)(1 \le v_i \le 10^{18})

All input values are integers.

The sum of KK over all test cases does not exceed 100 000100\,000.

Output

For each test case, print one line containing the length of the shortest path that starts at any vertex and visits each of the given KK vertices at least once.

Hint

A path of length LL in the tree is a sequence of visited vertices p0,p1,⋯ ,pLp_0, p_1, \cdots, p_L such that for each 1≤i≤L1 \le i \le L, pi−1p_{i-1} and pip_i are connected by an edge. You may visit the same vertex or edge more than once.

Examples1

  1. Example 1

    Input
    2
    3
    3 6 7
    4
    31 41 59 26
    
    Expected output
    2
    20