Under construction

Still being built. Things may change

Intruder Outsmarting

Interview

Time limit5sMemory limit1024 MB

Summary
Each wheel moves by plus or minus D on a cycle of N numbers. Find the fewest turns that make the wheel values a palindrome, or report that it is impossible.
Level

Medium5 of 10

Topics
Number theory, Math, Implementation
Solved
No attempts yet

Problem

The security key has WW wheels in a row. Each wheel has the numbers 11 through NN printed in order. One rotation moves the number shown on a wheel to the next or the previous number. Numbers wrap around, so the number after NN is 11, and the number before 11 is NN.

There is no hidden password. To activate the key, the numbers shown must form a palindrome, meaning the sequence reads the same from left to right and from right to left. To slow down intruders, the wheels only turn in steps of DD. In one operation, a wheel showing xx can be changed to show x−Dx - D or x+Dx + D. The wraparound works like this: if x−D<1x - D < 1, the number shown becomes x−D+Nx - D + N, and if x+D>Nx + D > N, the number shown becomes x+D−Nx + D - N.

Given the current number on each wheel, find the minimum number of operations needed to make the sequence of shown numbers a palindrome, or report that it is impossible.

Input

The first line contains the number of test cases TT. Each test case consists of two lines. The first line has three integers WW, NN, and DD. The second line has WW integers X1,X2,…,XWX_1, X_2, \dots, X_W, where XiX_i is the number currently shown on the ii-th wheel from the left.

Output

For each test case, output one line containing Case #x: y, where xx is the test case number starting from 1, and yy is the minimum number of operations needed to make the shown sequence a palindrome. If it is impossible, output IMPOSSIBLE instead of yy.

Limits

  • 1≤T≤1001 \le T \le 100.
  • 1≤D≤N−11 \le D \le N - 1.
  • 1≤Xi≤N1 \le X_i \le N, for all ii.

Hint

In Sample Case #1, the sequence can be made 5,4,5,4,55, 4, 5, 4, 5, which is a palindrome, with 33 operations: one addition on the first and fourth wheels, and one subtraction on the fifth wheel. No sequence of fewer moves works.

In Sample Case #2, the sequence is already a palindrome, so no operations are needed.

In Sample Case #3, both numbers must be equal for the sequence to be a palindrome. The wheel values only move by 22, and the two current numbers have different parity, so this cannot be done.

Examples1

  1. Example 1

    Input
    3
    5 5 4
    1 4 5 5 4
    3 4 2
    3 4 3
    2 4 2
    1 4
    
    Expected output
    Case #1: 3
    Case #2: 0
    Case #3: IMPOSSIBLE