{"problem":{"name":"A_A_i","description":{"content":"You are given a sequence $A = (A_1, A_2, \\ldots, A_N)$ of length $N$. Each element is either an integer between $1$ and $N$ (inclusive) or $-1$. Snuke has decided to create a new sequence using this s","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc209_d"},"statements":[{"statement_type":"Markdown","content":"You are given a sequence $A = (A_1, A_2, \\ldots, A_N)$ of length $N$. Each element is either an integer between $1$ and $N$ (inclusive) or $-1$.\nSnuke has decided to create a new sequence using this sequence $A$.\nFirst, replace all $-1$s in this sequence $A$ with integers between $1$ and $N$ (inclusive).\nNext, create a sequence $B = (B_1, B_2, \\ldots, B_N)$ of length $N$ according to the following formula:\n\n*   $B_i=A_{A_i}$\n\nFind the lexicographically smallest possible sequence $B$.\nYou are given $T$ test cases. Solve each of them.\nWhat is the lexicographical order of sequences?A sequence $C = (C_1,C_2,\\ldots,C_N)$ is **lexicographically smaller** than a sequence $D = (D_1,D_2,\\ldots,D_N)$ if and only if there exists an integer $1 \\leq i \\leq N$ such that both of the following hold:\n\n*   $(C_1,C_2,\\ldots,C_{i-1}) = (D_1,D_2,\\ldots,D_{i-1})$\n*   $C_i$ is (numerically) smaller than $D_i$.\n\n## Constraints\n\n*   $1 \\le T \\le 10^5$\n*   $1 \\le N \\le 5 \\times 10^5$\n*   $1 \\le A_i \\le N$ or $A_i = -1$.\n*   The sum of $N$ over all test cases is at most $5\\times 10^5$.\n*   All input values are integers.\n\n## Input\n\nThe input is given from Standard Input in the following format:\n\n$T$\n$case_1$\n$case_2$\n$\\vdots$\n$case_T$\n\nEach case is given in the following format:\n\n$N$\n$A_1$ $A_2$ $\\dots$ $A_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc209_d","tags":[],"sample_group":[["3\n4\n4 -1 -1 3\n5\n5 4 5 3 1\n7\n-1 6 5 -1 7 -1 6","3 1 4 1\n1 3 1 5 5\n1 1 7 1 6 1 1\n\nIn the first test case, the sequence $A$ before Snuke's replacement is $(4,-1,-1,3)$.\nSuppose he replaces it to get $A=(4, 3, 1, 3)$.\nThen, $B=(A_4, A_3, A_1, A_3)=(3, 1, 4, 1)$.\nIt is impossible to obtain a lexicographically smaller sequence $B$ than this."]],"created_at":"2026-03-03 11:01:14"}}