#include <bits/stdc++.h>
using namespace std;
#define TR(i,v)       for(__typeof((v).begin())i=(v).begin();i!=(v).end();++i)
#define DEBUG(x)      cout<<#x<<" = "<<x<<endl
#define SIZE(p)       (int)(p).size()
#define MP(a,b)       make_pair((a),(b))
#define ALL(p)        (p).begin(),(p).end()
#define rep(i,n)      for(int i=0;i<(int)(n);++i)
#define REP(i,a,n)    for(int i=(a);i<(int)(n); ++i)
#define FOR(i,a,b)    for(int i=(int)(a);i<=(int)(b);++i)
#define FORD(i,b,a)   for(int i=(int)(b);i>=(int)(a);--i)
#define CLR(x,y)      memset((x),(y),sizeof((x)))
#define fi first
#define se second
typedef long long LL;
typedef pair<int,int> pii;
int b[20][20];
void prt(){
  return;
  FORD(i,12,1){
    printf("%2d",i);
    FOR(j,1,9)putchar(b[i][j]?'*':' ');puts("");
  }
  printf("  ");
  FOR(j,1,9)printf("%d",j);puts("");
}
vector<pii> get_occupy(int x,int y,int kd,int o){
  vector<pii> r;
  if(kd==0){
    FOR(i,x,x+1)
    FOR(j,y,y+1)r.push_back(pii(i,j));
  }else if(kd==1){
    if(o==0){
      REP(i,x,x+4)r.push_back(pii(i,y));
    }else{
      REP(j,y,y+4)r.push_back(pii(x,j));
    }
  }else if(kd==2){
    if(o==0){
      REP(i,x,x+2)r.push_back(pii(i,y));
      FOR(j,y+1,y+2)r.push_back(pii(x,j));
    }else if(o==1){
      REP(i,x,x+3)r.push_back(pii(i,y));
      r.push_back(pii(x+2,y+1));
    }else if(o==2){
      REP(j,y,y+3)r.push_back(pii(x+1,j));
      r.push_back(pii(x,y+2));
    }else{
      REP(i,x,x+3)r.push_back(pii(i,y+1));
      r.push_back(pii(x,y));
    }
  }
  return r;
}
inline bool okin(int x,int y){
  return x>=1 && x<=12 && y>=1 && y<=9;
}
void clean_cell(int x,int y,int kd,int o){
  auto pos=get_occupy(x,y,kd,o);
  for(pii e:pos){
    int xx=e.fi,yy=e.se;
    if(okin(xx,yy)){      
      b[xx][yy]=0;
    }
  }
}
void set_sell(int x,int y,int kd,int o){
  auto pos=get_occupy(x,y,kd,o);
  for(pii e:pos){
    int xx=e.fi,yy=e.se;
    if(okin(xx,yy)){      
      b[xx][yy]=1;
    }
  }
}
bool illegal(int x,int y,int kd,int o){
  auto pos=get_occupy(x,y,kd,o);
  for(pii e:pos){
    int xx=e.fi,yy=e.se;    
    if(!okin(xx,yy) || b[xx][yy])return 1;
  }
  return 0;
}
void left(int &x,int &y,int kd,int o){
  clean_cell(x,y,kd,o);
  if(illegal(x,y-1,kd,o)){
    set_sell(x,y,kd,o);
    return;
  }  
  --y;
  set_sell(x,y,kd,o);
}
void right(int &x,int &y,int kd,int o){
  clean_cell(x,y,kd,o);
  if(illegal(x,y+1,kd,o)){
    set_sell(x,y,kd,o);
    return;
  }  
  ++y; 
  set_sell(x,y,kd,o);
}
void down(int &x,int &y,int kd,int o){
  clean_cell(x,y,kd,o);
  if(illegal(x-1,y,kd,o)){
    set_sell(x,y,kd,o);
    return;
  }  
  --x;
  set_sell(x,y,kd,o);
}
void up(int &x,int &y,int kd,int &o){
  clean_cell(x,y,kd,o);  
  int no;
  if(kd==1)no=(o+1)%2;
  if(kd==2)no=(o+1)%4;
  if(illegal(x,y,kd,no)){    
    set_sell(x,y,kd,o);
    return;
  }  
  o=no;
  set_sell(x,y,kd,o);
}
bool fall(int &x,int &y,int kd,int o){
  clean_cell(x,y,kd,o);
  if(illegal(x-1,y,kd,o)){
    set_sell(x,y,kd,o);
    return 0;  
  }
  --x;
  set_sell(x,y,kd,o);
  return 1;
}
void process_score(int &s){
  while(1){
    bool f=0;
    FOR(i,1,12){
      bool full=1;
      FOR(j,1,9)full&=b[i][j];
      if(full){
        ++s;
        REP(x,i,12)
        FOR(y,1,9)
          b[x][y]=b[x+1][y];
        FOR(y,1,9)b[12][y]=0;
        f=1;
        break;
      }
    }
    if(!f)break;
  }
}
int main(){
  int T;scanf("%d",&T);  
  FOR(cs,1,T){
    int nop;scanf("%d",&nop);
    queue<int> q;static char op[1005];
    scanf("%s",op);int cp=0,m=strlen(op);
    rep(i,nop){
      int x;scanf("%d",&x);q.push(x);
    }
    CLR(b,0);
    int score=0;
    while(!q.empty()){
      int u=q.front();q.pop();      
      int x=9,y=4,o=0;
      while(1){
        set_sell(x,y,u,o);
        prt();
        if(cp>=m)break;
        int cop=op[cp++];
        if(cop=='a')left(x,y,u,o);
        else if(cop=='s')down(x,y,u,o);
        else if(cop=='d')right(x,y,u,o);
        else if(cop=='w')up(x,y,u,o);
        if(!fall(x,y,u,o)){          
          break;  
        }
      }
      prt();
      process_score(score);
      prt();
    }
    printf("Case %d: %d\n",cs,score);
  }
  return 0;
}
