Problem: Legends who were born in 90s remember Mario very well. As we all are aware of that fabulous video game our little Mario tackling with all the problems and barriers coming his way , he finally saves his Queen from the giant monster Dragon.

In this problem we let you play the game but with a little twist. The aim is same though. You have to help out our Mario to save the Queen.

Twist is that you will be given a Matrix for current stage with characters including M (Position of Mario), Q (Position of Queen), $ (Position(s) of Dragon(s)) and .(dot) (Empty Positions).

Our Mario is asking you to tell him how many different and Unique moves he can make to save the Queen and then he will request you to tell him the very Optimized move (if there any).

So if you are really a big Mario Video Game fan then help out little Mario.

Input : First line will contains integer T indicating number of Test Cases. Each Test Case will have an integer N indicating the NxN matrix. Next N lines will be having the NxN matrix.

Output : For each Test Case produce the output as follows,
Case x: y < a1 a2 ... an > where x : Test Case Number, y : Optimized Step and < a1 a2 ... an > array in increasing order of all possible steps he can play.

Note : If there is no move to save the Queen just print -1.

Constraints

1 ≤ T ≤ 10

3 ≤ N ≤ 10

Note :- To be Eligible for Prizes you have to register and create your home address maptag.

Click here to create your Maptag

SAMPLE INPUT
2
3
. . Q
. . $
M $ .
3
M . .
$ $ .
Q $ .

SAMPLE OUTPUT
Case 1: 2 < 2 3 4 5 >
Case 2: -1

Explanation

Case 1 : Mario can save the Queen with steps < 2 3 4 5 > and the best answer will be 2

Case 2 : Mario can not save the Queen so answer is -1

Solution (in PYTHON):

Solution:
import copy
def cal(l,l1,path,x,y,c,n):
	#print(path)
	path.append((x,y))
	if(x-1>=0):
		if(y-1>=0 and (x-1,y-1) not in path):
			if(l[x-1][y-1]=='Q'):
				if(c not in l1):
					l1.append(c)
			if(l[x-1][y-1]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x-1,y-1,c+1,n)
		if((x-1,y) not in path):
			if(l[x-1][y]=='Q'):
				if( c not in l1):
					l1.append(c)
			if(l[x-1][y]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x-1,y,c+1,n)
		if(y+1<n and (x-1,y+1) not in path):
			if(l[x-1][y+1]=='Q'):
				if(c not in l1):
					l1.append(c)
			if(l[x-1][y+1]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x-1,y+1,c+1,n)
	if(y-1>=0 and (x,y-1) not in path):
		if(l[x][y-1]=='Q'):
			if(c not in l1):
				l1.append(c)
		if(l[x][y-1]=='.'):
			path1=copy.copy(path)
			cal(l,l1,path1,x,y-1,c+1,n)
	if(y+1<n and (x,y+1) not in path):
		if(l[x][y+1]=='Q'):
			if(c not in l1):
				l1.append(c)
		if(l[x][y+1]=='.'):
			path1=copy.copy(path)
			cal(l,l1,path1,x,y+1,c+1,n)
	if(x+1<n):
		if(y-1>=0 and (x+1,y-1) not in path):
			if(l[x+1][y-1]=='Q'):
				if(c not in l1):
					l1.append(c)
			if(l[x+1][y-1]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x+1,y-1,c+1,n)
		if((x+1,y) not in path):
			if(l[x+1][y]=='Q'):
				if( c not in l1):
					l1.append(c)
			if(l[x+1][y]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x+1,y,c+1,n)
		if(y+1<n and (x+1,y+1) not in path):
			if(l[x+1][y+1]=='Q'):
				if(c not in l1):
					l1.append(c)
			if(l[x+1][y+1]=='.'):
				path1=copy.copy(path)
				cal(l,l1,path1,x+1,y+1,c+1,n)	
	
t=int(raw_input())
for i in range(1,t+1):
	n=int(raw_input())
	l=[]
	for j in range(n):
		s=raw_input().split()
		l.append(s)
	l1=[]
	for j in range(n):
		for k in range(n):
			if(l[j][k]=='M'):
				xindex=j
				yindex=k
	# print(l)
	# print(xindex,yindex)
	cal(l,l1,[],xindex,yindex,1,n)
	l1.sort()
	if(l1==[]):
		print("Case "+str(i)+ ": -1")
	else:
		s=""
		for j in range(len(l1)):
			s+=" "+str(l1[j])
		print("Case "+str(i)+": "+str(l1[0])+" <"+s+" >")