Under construction

Still being built. Things may change

Rectangle

Time limit1sMemory limit1024 MB

Summary
Given N unit squares on distinct grid cells, find the smallest possible perimeter of an axis-parallel rectangle that contains all of them, for each N up to 1e9.
Level

Medium4 of 10

Topics
Math, Implementation
Solved
No attempts yet

Problem

There is an infinitely large square grid. You want to place NN squares of size 1×11 \times 1, each on a different cell of this grid.

Call a shape a minimum bounding rectangle if it satisfies the following conditions and has the shortest perimeter among such shapes.

  • The shape is a rectangle whose sides are all parallel to the grid lines.
  • The shape contains all of the placed squares.

Among all ways to place NN squares, in which case is the perimeter of the minimum bounding rectangle the shortest?

Input

The first line contains the number of test cases TT (1≤T≤2×1051 \le T \le 2 \times 10^5).

Each of the next TT lines contains one integer NN, the number of squares to place (1≤N≤1091 \le N \le 10^9).

Output

For each test case, print the shortest possible perimeter of the minimum bounding rectangle.

Hint

This problem has a large input, so you may need to use fast I/O to avoid a time limit error.

  • If you use C++ and want to use cin/cout:

    • Put cin.tie(nullptr) and ios::sync_with_stdio(false) at the top of main.
    • Use the newline character (\n) instead of endl.
    • In this case, you must not use C I/O such as scanf/printf/puts/getchar/putchar.
  • If you use Java:

    • Use BufferedReader and BufferedWriter instead of Scanner and System.out.println.
    • Call BufferedWriter.flush once at the end.
  • If you use Python:

    • Use sys.stdin.readline instead of input. Note that this function also reads the trailing newline.
    • It is a good idea to add .rstrip() if you want to store a string without the newline.

Examples1

  1. Example 1

    Input
    3
    9
    14
    33
    
    Expected output
    12
    16
    24