Under construction

Still being built. Things may change

Bracket String Notation (Small)

Interview

Time limit1.5sMemory limit1024 MB

Summary
Given N, find the shortest bracket string whose value is N, breaking length ties by lexicographic order.
Level

Medium5 of 10

Topics
Dynamic programming, Math
Solved
No attempts yet

Problem

A bracket string is a string that can be built only with these three rules.

  • The empty string is a bracket string.
  • For a bracket string aa, (aa) is a bracket string.
  • For two bracket strings aa and bb, abab is a bracket string.

Examples of bracket strings include (())(), (((()())())()), and ((()())()()())(()).

The value of a bracket string is determined as follows.

  • The value of the empty string is 1.
  • If the value of a bracket string aa is xx, then the value of (aa) is x+1x + 1.
  • If the values of two bracket strings aa and bb are xx and yy, respectively, then the value of abab is x×yx \times y.

For example, (())() represents 6.

  • () represents 1+1=21 + 1 = 2.
  • (()) represents 2+1=32 + 1 = 3.
  • (())() represents 3×2=63 \times 2 = 6.

Given a natural number NN, find the shortest bracket string that represents NN. If several such strings exist, find the one that comes first in lexicographic order.

Input

The first line contains the number of test cases TT. (1≤T≤201 \le T \le 20)

From the second line on, each line contains one natural number NN. (2≤N≤1,000,0002 \le N \le 1{,}000{,}000)

Output

For each test case, print on its own line the shortest bracket string that represents NN. If several such strings exist, print the one that comes first in lexicographic order.

Examples2

  1. Example 1

    Input
    3
    6
    23
    123
    
    Expected output
    (())()
    (((()())())())
    ((()())()()())(())
    
  2. Example 2

    Input
    6
    987997
    9879
    98799
    98
    987
    9
    
    Expected output
    ((((((()()()()()()()())()())(()())())())()())(())()())
    ((((()()()())()()())(())())()())(())
    ((((()()()()()()()())()())()()())()())(())
    ((())()()()())()
    (((()())()()())()()())(())
    (()()())