import math
def min_operations_to_make_gcd(A, X, N):
total_operations = 0
for i in range(N):
if math.gcd(A[i], X) > 1:
continue # No operations needed, this element is already valid
# If gcd(A[i], X) is not valid, check how many increments are needed
diff = X - A[i] % X
total_operations += diff if diff != X else 0 # Only add the remainder to make it divisible
return total_operations
def solve():
# Read the number of test cases
T = int(input())
for _ in range(T):
# Read the test case input values
N, X = map(int, input().split())
A = list(map(int, input().split()))
# Split the array into two halves
half_size = N // 2
# First half should be (X+1)-ordinary
first_half = A[:half_size]
# Second half should be X-ordinary
second_half = A[half_size:]
# Check if we can make the first half (X+1)-ordinary
first_half_operations = min_operations_to_make_gcd(first_half, X + 1, half_size)
# Check if we can make the second half X-ordinary
second_half_operations = min_operations_to_make_gcd(second_half, X, N - half_size)
# If we can make both halves ordinary, return the total number of operations
if first_half_operations != -1 and second_half_operations != -1:
print(first_half_operations + second_half_operations)
else:
print(-1)