#P11671. [USACO25JAN] Farmer John's Favorite Operation S

[USACO25JAN] Farmer John's Favorite Operation S

Description

It is another cold and boring day on Farmer John's farm. To pass the time, Farmer John has invented a fun leisure activity involving performing operations on an integer array.

Farmer John has an array aa of NN (1N21051 \leq N \leq 2 \cdot 10^5) non-negative integers and an integer MM (1M1091 \leq M \leq 10^9). Then, FJ will ask Bessie for an integer xx. In one operation, FJ can pick an index ii and subtract or add 11 to aia_i. FJ's boredom value is the minimum number of operations he must perform so that aixa_i-x is divisible by MM for all 1iN1 \leq i \leq N.

Among all possible xx, output FJ's minimum possible boredom value.

Input Format

The first line contains TT (1T101 \leq T \leq 10), the number of independent test cases to solve.

The first line of each test case contains NN and MM.

The second line of each test case contains a1,a2,...,aNa_1, a_2, ..., a_N (0ai1090 \leq a_i \leq 10^9).

It is guaranteed that the sum of NN over all test cases does not exceed 51055 \cdot 10^5.

Output Format

For each test case, output an integer on a new line containing FJ's minimum possible boredom value among all possible values of xx.

2
5 9
15 12 18 3 8
3 69
1 988244353 998244853
10
21

Hint

In the first test case, one optimal choice of xx is 33. FJ can perform 1010 operations to make a=[12,12,21,3,12]a = [12, 12, 21, 3, 12].

SCORING:

  • Input 2: N1000N \le 1000 and M1000M \le 1000.
  • Input 3: N1000N\le 1000.
  • Inputs 4-5: M105M\le 10^5.
  • Inputs 6-16: No additional constraints.