import sys input = sys.stdin.readline def gcd(a, b): while b: a, b = b, a % b return a from operator import itemgetter import math from functools import lru_cache @lru_cache(maxsize=None) def euler_totient(x): ANS=x # 素因数分解 L=int(math.sqrt(x)) FACT=dict() for i in range(2,L+2): while x%i==0: FACT[i]=FACT.get(i,0)+1 x=x//i if x!=1: FACT[x]=FACT.get(x,0)+1 # φ(x)=x(1-1/p_1)...(1-1/p_m)という性質を使って計算 for f in FACT: ANS=ANS*(f-1)//f return ANS T=int(input()) for tests in range(T): W,N,D=map(int,input().split()) X=list(map(int,input().split())) ANS=0 for i in range(W//2): x=X[i] y=X[len(X)-1-i] if x==y: continue else: k=(x-y)%N l=(y-x)%N GCD=gcd(gcd(k,D),N) a=D//GCD k//=GCD l//=GCD mod=N//GCD if gcd(a,mod)!=1: ANS+=1<<60 else: phi=euler_totient(mod) sc=k*pow(a,phi-1,mod)%mod sc2=l*pow(a,phi-1,mod)%mod ANS+=min(sc,sc2) if ANS>=(1<<60): ANS="IMPOSSIBLE" print("Case #"+str(tests+1)+": "+str(ANS))