# Google Code Jam 2023 Farewell Round B - Intruder Outsmarting def gcd(a, b): if a % b == 0: return b else: return gcd(b, a % b) # solve ax + by = gcd(a, b) # return (gcd(a, b), x, y) # require a>=0, b>=0 def extgcd(a, b): if b==0: return (a, 1, 0) else: t = extgcd(b, a%b) return (t[0], t[2], t[1] - (a//b)*t[2]) def mod_inverse(a, mod): r = extgcd(a, mod) return (r[1] % mod + mod) % mod t = int(input()) for case in range(1, t + 1): w, n, d = map(int, input().split()) x = [v - 1 for v in list(map(int, input().split()))] g = gcd(n, d) n //= g d //= g ans = 0 for i in range(w//2): d1 = abs(x[i]-x[w-i-1]) if d1 % g != 0: ans = "IMPOSSIBLE" break d1 //= g d2 = n-d1 ans += min((d1*mod_inverse(d, n))%n, (d2*mod_inverse(d, n))%n) print("Case #{}: {}".format(case, ans))