#include <stdio.h>
#include <string.h>
#include <math.h>

#define eps 0.00001
#define NCHARS 30
#define MAXLEN 10000
#define SCALE 9
#define MAIN (NCHARS-1)

struct ccoords{double cx,cy, dx,dy, w,q; int da;};

struct ccoords D[2][NCHARS];
char g[NCHARS][MAXLEN];

double SIN(int a){return sin(a*M_PI/6);}
double COS(int a){return cos(a*M_PI/6);}

void next(int t)
{
  int f=1-t;
  int i;
  double maxw=0.0;

  for(i=0;i<NCHARS;i++)
  {
    double cxw=0.0,cyw=0.0, dx=0.0,dy=0.0, w=0.0,q=1.0;
    int a=0,j;
    for(j=0;g[i][j];j++)
    {
      if(g[i][j]=='@')
      {
        double x;
        sscanf(g[i]+j+2,"%lf",&x);
        while(g[i][j]!='}')j++;
        q*=x;
      }
      else
      {
        int c=g[i][j]-'a';
        cxw+=(dx+(D[f][c].cx*COS(a)-D[f][c].cy*SIN(a))*q)  *D[f][c].w*q;
        cyw+=(dy+(D[f][c].cx*SIN(a)+D[f][c].cy*COS(a))*q)  *D[f][c].w*q;
        dx+=(D[f][c].dx*COS(a)-D[f][c].dy*SIN(a))*q;
        dy+=(D[f][c].dx*SIN(a)+D[f][c].dy*COS(a))*q;
        w+=D[f][c].w*q;
        q*=D[f][c].q;
        a+=D[f][c].da;
        a=a%12;
      }
    }
    if(w>eps){D[t][i].cx=cxw/w; D[t][i].cy=cyw/w;}
    else{D[t][i].cx=0.0; D[t][i].cy=0.0;}
    D[t][i].dx=dx; D[t][i].dy=dy;
    D[t][i].w=w; D[t][i].q=q;
    D[t][i].da=a;
    if(w>maxw)maxw=w;
  }
}

void init()
{
  int i;
  for(i=0;i<NCHARS;i++){g[i][0]=i+'a',g[i][1]=0;}
  for(i=0;i<NCHARS;i++)
  {
    D[0][i].cx=0.0; D[0][i].cy=0.0;
    D[0][i].dx=0.0; D[0][i].dy=0.0;
    D[0][i].w=0.0; D[0][i].q=1.0;
    D[0][i].da=0;
  }
  D[0]['f'-'a'].cy=0.5;
  D[0]['f'-'a'].dy=1.0;
  D[0]['f'-'a'].w=1.0;
  D[0]['l'-'a'].da=1;
  D[0]['r'-'a'].da=-1;
}

void solve1()
{
  int K,L,i;
  char s[MAXLEN+7];
  init();
  scanf("%d %d ",&K,&L);
  scanf("%s ",g[MAIN]);
  for(i=0;i<L;i++){scanf("%s ",s); strcpy(g[s[0]-'a'],s+2);}
  for(i=0;i<=K;i++)next((i+1)%2);
  printf("%.*lf %.*lf\n",SCALE,D[(K+1)%2][MAIN].cx,SCALE,D[(K+1)%2][MAIN].cy);
}

int main()
{
  int N;
  scanf("%d ",&N);
  while(N--)solve1();
  return 0;
}
