Concat (2nd)

AtCoder
IDabc434_f
Time2000ms
Memory256MB
Difficulty
You are given $N$ strings $S_i$ consisting of lowercase English letters. For all possible permutations $P=(P_1,P_2,\dots,P_N)$ of $(1,2,\dots,N)$, write down the string generated as follows: * Concatenate $S_{P_1},S_{P_2},\dots,S_{P_N}$ in this order. Let $A_1,A_2,\dots,A_{N!}$ be the sequence of the $N!$ written strings sorted in lexicographical order. Output $A_2$. You are given $T$ test cases; solve each of them. ## Constraints * $1 \le T \le 1.5 \times 10^5$ * $2 \le N \le 3 \times 10^5$ * $T,N$ are integers. * $S_i$ is a string consisting of lowercase English letters with length between $1$ and $10^6-1$, inclusive. * For a single input, the sum of $N$ does not exceed $3 \times 10^5$. * For a single input, the sum of $|S_i|$ does not exceed $10^6$. ## Input The input is given from Standard Input in the following format: $T$ $\text{case}_1$ $\text{case}_2$ $\vdots$ $\text{case}_T$ Each test case is given in the following format: $N$ $S_1$ $S_2$ $\vdots$ $S_N$ [samples]
Samples
Input #1
3
3
abc
ac
ahc
4
aaa
a
aaaa
a
15
ks
sy
k
ysk
yks
ky
ksy
sk
syk
s
kys
sky
ys
yk
y
Output #1
abcahcac
aaaaaaaaa
kksksykykysskskyssyksyyksykyskyys

This input contains three test cases.
For the first test case, $S=($ `abc`, `ac`, `ahc` $)$.  
We have $A=($ `abcacahc`, `abcahcac`, `acabcahc`, `acahcabc`, `ahcabcac`, `ahcacabc` $)$, so output $A_2=$ `abcahcac`.
API Response (JSON)
{
  "problem": {
    "name": "Concat (2nd)",
    "description": {
      "content": "You are given $N$ strings $S_i$ consisting of lowercase English letters. For all possible permutations $P=(P_1,P_2,\\dots,P_N)$ of $(1,2,\\dots,N)$, write down the string generated as follows: *   Conc",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc434_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "You are given $N$ strings $S_i$ consisting of lowercase English letters.\nFor all possible permutations $P=(P_1,P_2,\\dots,P_N)$ of $(1,2,\\dots,N)$, write down the string generated as follows:\n\n*   Conc...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments