
import java.awt.Point;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
 
 
 
 
 
 class TestClass {
 
static int[] dist ;	
static int[][][] vis;
static ArrayList<Integer>[] resu;
static char[][] graph;
static int max;
static int min;
static int[][][] dp;
static int zz;
static int zzz;
static boolean fin;


static class facebook{
	
	int x ;
	int y ;
	int d;
	int moves;
	
	public  facebook(int x , int y , int d , int moves){
		   this.x = x;
		   this.y = y;
		   this.d =d;
		   this.moves = moves;
	}
	
}

public static boolean check_s(int i , int j){
	
	if(graph[i][j]==0 ||graph[i][j]==1 || graph[i][j]==2 || graph[i][j]==3 ) return true;
	
	return false;
}


public static void check(int x , int y , int moves){
	
        if(moves==0){
        	x--;
          while(x>=0){
        	  
        	 if(graph[x][y]== '#' || check_s(x, y)) break;
        	 else{
        		  dp[x][y][0] = Integer.MAX_VALUE;
        		  x--;
        	 }
          }
        }
        else if(moves==1){
        	   y++;
        	   while(y<graph[0].length){
        			 if(graph[x][y]== '#' || check_s(x, y)) break;
        			 else{
        				 dp[x][y][1] = Integer.MAX_VALUE;
        				 y++;
        			 }
        	   }
        	
        }
        else if(moves==2){
        	x++;
        	while(x<graph.length){
        		 if(graph[x][y]== '#' || check_s(x, y)) break;
        		 else{
        			 dp[x][y][2] =Integer.MAX_VALUE;
        			 x++;
        		 }
        	}
        }
        else if(moves==3){
        	y--;
        	while(y>=0){
        		 if(graph[x][y]== '#' || check_s(x, y)) break;
        		 else{
        			 dp[x][y][3] =Integer.MAX_VALUE;
        			 y--;
        		 }
        	}
        }
	
	
}
public static int visitedcheck(int i , int j , int moves){
	  
	if(zz==-1) return 0;
	
	int xx = graph[zz][zzz]+moves;
	return (xx%4);
}

public static boolean iscorrect(int i , int j , int moves){
	int temp;
	  temp=i-1;
	   while(temp>=0){
		   
		   if(graph[temp][j]== '#') break;
		   else if(check_s(temp, j)){
			   
			   int xx = (graph[temp][j]+ moves)%4;
			   if(dp[i][j][xx]==Integer.MAX_VALUE) return false;
			   else break;
		   }
		     temp--;
			   
		   
	   }
	  
	   temp = i+1;
	   while(temp<graph.length){
		   
		   if(graph[temp][j]== '#') break;
		   else if(check_s(temp, j)){
			   
			   int xx = (graph[temp][j]+ moves)%4;
			   if(dp[i][j][xx]==Integer.MAX_VALUE) return false;
			   else break;
		   }
		     temp++;
	   }
	   
	   temp = j+1;
	   while(temp<graph[0].length){
		   
		   if(graph[i][temp]== '#') break;
		   else if(check_s(i, temp)){
			   
			   int xx = (graph[i][temp]+ moves)%4;
			  
			   if(dp[i][j][xx]==Integer.MAX_VALUE) return false;
			   else break;
		   }
		     temp++;
	   }
	   temp = j-1;
	   while(temp>=0){
		   if(graph[i][temp]== '#') break;
		   else if(check_s(i, temp)){
			   
			   int xx = (graph[i][temp]+ moves)%4;
			   if(dp[i][j][xx]==Integer.MAX_VALUE) return false;
			   else break;
		   }
		     temp--;
	   }
	   
	   
	
	 return true;
}

public static void findanswer(int current_x , int current_y , int final_x, int final_y){
	
	  
	  int[] x = {1,-1,0,0};
	  int[] y = {0,0,1,-1};
	 vis[current_x][current_y][visitedcheck(current_x, current_y, 0)] =1;
	  facebook obj = new facebook(current_x, current_y, 0, 0);
	  LinkedList<facebook> Q = new LinkedList<TestClass.facebook>();
	  Q.add(obj);
	  
	  while(!Q.isEmpty()){
		  
		  int xx =  Q.peek().x;
		  int yy = Q.peek().y;
		  int d = Q.peek().d;
		  int mov = Q.peek().moves;
		  Q.pop();
		  
		  for(int i=0;i<4;i++){
			  
			  int dx = xx + x[i];
			  int dy = yy + y[i];
			  int moves = mov+1;
			  int dist = d+1;
	if(dx>=0 && dy>=0 && dx<graph.length && dy<graph[0].length && graph[dx][dy]!='#' &&!check_s(dx, dy) && vis[dx][dy][visitedcheck(dx, dy, moves)]!=1){
		
		             if(iscorrect(dx, dy, moves)){
		            	// System.out.println(dx+" "+dy+" "+moves);
		            	 if(dx==final_x && dy== final_y){
		            		 if(max>dist) max=dist;
		            		// System.out.println(dist);
		            		 //vis[dx][dy][visitedcheck(dx, dy, moves)]=1;
		            	 }else{
		            	 obj = new facebook(dx, dy, d+1, moves);
		            	 Q.add(obj);
		            	 vis[dx][dy][visitedcheck(dx, dy, moves)]=1;
		            	 
		            	 }
		            	 
		             }
	}
		  }
		  
	  }
	  
	  	  
	
  
			     
		  
		  

}


public static void main(String args[] ) throws IOException  {
 

Scanner in = new Scanner(new InputStreamReader(System.in));
int t = in.nextInt();
for(int i=1;i<=t;i++){
	int n =in.nextInt();
	int m = in.nextInt();
	graph = new char[n][m];
	dp = new int[n][m][4];
	vis = new int[n][m][4];
	int sourcex = 0 , sourcey=0;
	int distx=0 , disty=0;
	zz= -1; zzz=-1;
	
	for(int j=0;j<n;j++)
	{	
		graph[j] =  in.next().toCharArray();
		for(int k=0;k<m;k++)
		{
			if(graph[j][k]=='S'){
				sourcex = j;
				sourcey =k;
			}
			
			if(graph[j][k] == 'G')
			{
				 distx=j;
				 disty=k;
			}
			if(graph[j][k]=='<') {graph[j][k]=3;zz=j; zzz=k; }
			if(graph[j][k]=='>') {graph[j][k]=1;zz=j; zzz=k;}
			if(graph[j][k]=='^') {graph[j][k]=0; zz=j; zzz=k;}
			if(graph[j][k]=='v') {graph[j][k]=2; zz=j; zzz=k;}
		}
	
	
	}
	for(int j=0;j<n;j++)
		for(int k=0;k<m;k++){
			
			if(check_s(j, k)){
		     
			for(int mm=0;mm<4;mm++)
			  check(j, k, mm);
		     
			}
		}
	max =Integer.MAX_VALUE;
	findanswer(sourcex, sourcey, distx, disty);

    if(max!=Integer.MAX_VALUE)
	System.out.println("Case #"+i+": "+max);
    else
    	System.out.println("Case #"+i+": impossible");
    //pw.flush();
	//System.out.println(iscorrect(1, 2, 2) +" " +dp[1][2][2]);
	
		
	
				 
	
}



}
 }
 