/*
    題意:給出一個4*6的網格,有3種顏色,每種8個,先給出網格求最少步數使得中間8個同一顏色
    操作是對每一行、列shift
    
    解題報告:
    "BFS will be worked at this problem.
    Since the final state is determinate, we can do BFS at first to find out the answer for all states.
    The state is only decide by the locations of the 8 squares you choose. It is Combination(24,8)"

    由于最終狀態確定的,所以可以從目標狀態搜出去,預處理所有情況
    更精彩的是,狀態總共只有C(24,8)種!
    把最終處在中間的那8個格子的顏色看為1,其余的看為0,這樣那8個格子在24個網格的分布就是狀態了
    反過來,確定要把某8個格子移到中間,其余顏色的都是不管的,所以就8個1,16個0

    我的寫法超時,寫的好搓,對于shift,我是一個一個swap

    請教了Resty的寫法,很不錯,很快,以下代碼是參考他的
    
    左移,就是next[i] = now[i+1]
    右移,就是next[i] = now[i-1]

    可以預先把位置存下來,如第二行就6 7 8 9 10 11
    然后next[pos[i]] = now[pos[i+1]]就行了
    對于右移,再存多一個反過來的,即11 10 9 8 7 6
    這樣next[pos[i]] = now[pos[i+1]]也是正確的

*/

#include
<cstdio>
#include
<iostream>
#include
<algorithm>
#include
<cstring>

using namespace std;

const int MAXN = 735471//C(24,8)
const int INF = 1000000000;

#define move(i) (1<<(i))
#define get(x,i) (x&(1<<(i)))

char step[1<<24];//0 for haven't sovle
int Q[MAXN+10];
int num[20] , pos[20][7];//20種操作


int change(int now , int id)
{
    
int next = now;
    
int *= pos[id] , n = num[id];
    
for(int i = 0; i < n ; i++)//next[p[i]] = now[p[i+1]]
    {
        
int nowPos = p[(i+1)%n] , nextPos = p[i];
        
if(get(now,nowPos))next |= move(nextPos);
        
else next ^= get(next,nextPos);//set 0
    }

    
return next;
}


void init()
{
    
//row pos
    
// 0 1 2 3 4 5
    
// 5 4 3 2 1 0
    for(int id = 0 ; id < 8 ; id++)
    
{
        num[id] 
= 6;
        
for(int j = 0; j < 6 ; j++)
        
{
            pos[id][j] 
= id/2*6 + j;
        }

        
if(id&1)reverse(pos[id],pos[id]+6);
    }

    
//col pos
    
// 0 18
    
// 6 12  
    
// 12 6
    
// 18 0 
    for(int id = 8 ; id < 20 ; id++)
    
{
        num[id] 
= 4;
        
for(int j = 0 ; j < 4 ; j++)
        
{
            pos[id][j] 
= 6*+ (id-8)/2;
        }

        
if(id&1)reverse(pos[id],pos[id]+4);
    }


    
int start = (15<<13+ (15<<7);
    step[start] 
= 1;//
    int front = 0 , tail = 0;
    Q[tail
++= start;
    
while(front < tail)
    
{
        
int state = Q[front++];
        
for(int id = 0 ; id < 20 ; id++)
        
{
            
int nextState = change(state,id);
            
if(step[nextState] == 0)
            
{
                step[nextState] 
= step[state]+1;
                Q[tail
++= nextState;
            }

        }

    }

}


int main()
{

#ifdef ONLINE_JUDGE
#else 
    freopen(
"in","r",stdin);
#endif

    init();

    
char str[4][7] , ch[3= {'G','W','B'};

    
int T , t = 1;
    
for( scanf("%d",&T) ; T -- ; )
    
{
        
for(int i = 0 ; i < 4; i++)
        
{
            scanf(
"%s",str[i]);
        }

        
int ans = INF;
        
for(int k = 0 ; k < 3 ; k++)
        
{
            
int state = 0;
            
for(int i = 0 ; i < 4 ; i++)
                
for(int j = 0 ; j < 6 ; j++)
                    
if(str[i][j] == ch[k] )state |= move(6*i+j);
            ans 
= min(ans , (int)step[state]);
        }

        printf(
"Case %d: %d\n",t++,ans-1);
    }

    
return 0;
}