{"problem":{"name":"Match, Mod, Minimize","description":{"content":"You are given sequences of non-negative integers $A=(A_1,A_2,\\dots ,A_N),B=(B_1,B_2,\\dots ,B_N)$ of length $N$ and a positive integer $M$. When the elements of $A$ can be rearranged freely, find the m","description_type":"Markdown"},"platform":"AtCoder","limit":{"time_limit":2000,"memory_limit":262144},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"arc201_d"},"statements":[{"statement_type":"Markdown","content":"You are given sequences of non-negative integers $A=(A_1,A_2,\\dots ,A_N),B=(B_1,B_2,\\dots ,B_N)$ of length $N$ and a positive integer $M$.\nWhen the elements of $A$ can be rearranged freely, find the minimum possible value of $\\max_{1\\le i\\le N} ((A_i+B_i) \\bmod M)$.\n$T$ test cases are given, so find the answer for each.\n\n## Constraints\n\n*   $1 \\le T \\le 10^5$\n*   $1 \\le N \\le 3 \\times 10^5$\n*   $1 \\le M \\le 10^9$\n*   $0 \\le A_i,B_i \\lt M$\n*   The sum of $N$ over all test cases is at most $3 \\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$\\text{case}_1$\n$\\text{case}_2$\n$\\vdots$\n$\\text{case}_T$\n\nEach test case is given in the following format:\n\n$N$ $M$\n$A_1$ $A_2$ $\\ldots$ $A_N$\n$B_1$ $B_2$ $\\ldots$ $B_N$\n\n[samples]","is_translate":false,"language":"English"}],"meta":{"iden":"arc201_d","tags":[],"sample_group":[["3\n3 6\n3 1 4\n2 0 1\n1 1000000000\n999999999\n999999999\n10 201\n144 150 176 154 110 187 38 136 111 46\n96 109 73 63 85 1 156 7 13 171","3\n999999998\n111\n\nFor the first test case, if we rearrange $A$ to $4,3,1$, then $(A_i+B_i) \\bmod M$ becomes $0,3,2$ respectively, and the $\\max$ of these is $3$."]],"created_at":"2026-03-03 11:01:14"}}