P17537 [JAG 2026 Summer Camp #1] Rabbit, Rabbit, Rabbit
Description
There are $N$ rabbits and $N$ watering holes on a number line, where the positive direction is to the right. The $i$-th rabbit is initially at coordinate $X_i$, and the $i$-th watering hole is at coordinate $Y_i$. You may perform the following operation zero or more times:
- For each of the $N$ rabbits, independently move it by a distance of $1$ to either the right or the left. If $R$ rabbits move to the right and $L$ rabbits move to the left, this operation incurs a cost of $R\times L$.
Note that multiple rabbits may occupy the same coordinate simultaneously.
Your objective is to reach a state where the coordinates of the $N$ rabbits match the coordinates of the $N$ watering holes in some order. Find the minimum total cost required to achieve this objective. If it is impossible, output `-1`.
Input Format
The input contains one or more test cases. The first line of the input contains an integer $T$ ($1\le T\le 3\times 10^5$), which is the number of test cases. The descriptions of the $T$ test cases follow, each in the following format.
```text
N
X_1 X_2 ... X_N
Y_1 Y_2 ... Y_N
```
The first line contains an integer $N$ ($1\le N\le 3\times 10^5$).
The second line contains $N$ integers $X_1,X_2,\ldots,X_N$ ($-10^7\le X_i\le 10^7$). $X_i$ represents the initial coordinate of the $i$-th rabbit.
The third line contains $N$ integers $Y_1,Y_2,\ldots,Y_N$ ($-10^7\le Y_i\le 10^7$). $Y_i$ represents the coordinate of the $i$-th watering hole. It is guaranteed that $Y_i\ne Y_j$ for all $i\ne j$.
The sum of $N$ over all test cases does not exceed $3\times 10^5$.
Output Format
For each test case, output the minimum total cost required to achieve the objective. If it is impossible, output `-1`.