#include <cstdio>
#include <cstring>
#define MAXX 12
#define MAXY 9
#define SIZE 4

int tcase,n,q,ans,lst[1010];
char op[1010];

short BLOCK[3][4][4][4] = 
{
    {{1,1,0,0,1,1,0,0}},
    {{1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0},{1,1,1,1}},
    {{1,1,1,0,1,0,0,0},{1,0,0,0,1,0,0,0,1,1,0,0},{0,0,1,0,1,1,1,0},{1,1,0,0,0,1,0,0,0,1,0,0}}
};
int siz[3] = {1,2,4};
short MAP[30][20];

bool check(int x,int y,int kind,int dir)
{
    int i,j;
    for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++)
        if(MAP[x+i][y+j] && BLOCK[kind][dir][i][j]) return false;
    return true;
}

void putBlock(int x,int y,int kind,int dir)
{
    int i,j;
    for(i=0;i<SIZE;i++) for(j=0;j<SIZE;j++)
        MAP[x+i][y+j] |= BLOCK[kind][dir][i][j];
}

void fall()
{
    int i,j,cnt,bias = 0;
    for(i=1;i<=2*MAXX;i++)
    {
        cnt = 0;
        for(j=1;j<=MAXY;j++) cnt += MAP[i][j];
        for(j=1;j<=MAXY;j++) MAP[i-bias][j] = MAP[i][j];
        if(cnt == MAXY) ans++, bias++;
    }
}

void work()
{
    int i,x,y,kind,dir;
    bool newBlock = true;
    
    q = 0; ans = 0;
    memset(MAP,0,sizeof(MAP));
    for(i=0;i<=MAXX;i++) MAP[i][0] = MAP[i][MAXY+1] = 1;
    for(i=0;i<=MAXY;i++) MAP[0][i] = 1;
    
    while(op[++q])
    {
        if(newBlock)
        {
            x = 9; y = 4;
            kind = lst[n--]; dir = 0;
            newBlock = false;
        }
        
        if(op[q] == 'w' && check(x,y,kind,(dir+1)%siz[kind]))
            dir = (dir+1)%siz[kind];
        else if(op[q] == 'a' && check(x,y-1,kind,dir))
            y--;
        else if(op[q] == 's' && check(x-1,y,kind,dir))
            x--;
        else if(op[q] == 'd' && check(x,y+1,kind,dir))
            y++;
        
        if(check(x-1,y,kind,dir))
            x--;
        else
        {
            putBlock(x,y,kind,dir);
            fall();
            newBlock = true;
        }
    }
}

int main()
{
    int i;
    
    scanf("%d",&tcase);
    for(int cas=1;cas<=tcase;cas++)
    {
        scanf("%d%s",&n,op+1);
        for(i=n;i;i--) scanf("%d",&lst[i]);
        
        work();
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}
