#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

#define MAXR 131072

#define MAXSTAVU 6000
#define MAXKROKU 100000
#define MAXP 10
#define MAXW 1000
#define MAXM 20

struct { int stav; char znak, smer; } stavy[MAXSTAVU][MAXM+2]; /* 0 -> -1, atd. */
char paska[MAXR];
int prom[MAXP];

int comp(char * ex, int b)
{
/*  int c;
  c=ex[b];
  ex[b]=0;
  printf("%s\n", ex);
  ex[b]=c; */
  if (b==1) return prom[ex[0]-'1']; else
  {
    int pl = -1, mu = -1, v=0, i;
    for (i=0; i<b; i++)
     if (ex[i]=='(') v++; else
     if (ex[i]==')') v--; else
     if ((v==0)&&(ex[i]=='+')) pl=i; else
     if ((v==0)&&(ex[i]=='*')) mu=i;
     
    if (pl!=-1) return (comp(ex, pl) + comp(ex+pl+1, b-1-pl)) % 10; else
    if (mu!=-1)
      return (comp(ex, mu) * comp(ex+mu+1, b-1-mu)) % 10; else
    if ((ex[0]!='(')||(ex[b-1]!=')')) printf("ERR\n"); else
      return comp(ex+1, b-2);
  }
  return 0;
}

void sigal(int s)
{
  if (s==-1)
  {
  printf("Wrong answer!\n");
  exit(2);
  }
}


int main(int argc, char ** argv)
{
  int i, j, m, n, s1, c1, c2, s2, kroky, stav, pos, poc;
  char sm;
  char st[MAXW];
  FILE * fi1, * fi2;
  
    fi1=fopen(argv[1], "rt");
    fi2=fopen(argv[3], "rt");
    
    poc=-1;
    fscanf(fi1, "%d\n", &poc);
    
    while (poc-->0)
    {
      fscanf(fi1, "%s\n", st);
      fscanf(fi2, "%d %d\n", &m, &n);
      for (i=0; i<MAXR; i++) paska[i]=-1;
      for (i=0; i<MAXSTAVU; i++)
       for (j=0; j<MAXM+2; j++)
        stavy[i][j].stav=-1;
        
      if ((m<0) || (m>MAXM) || (n<0) || (n>MAXSTAVU))
      {
         printf("Presentation error!\n");
         exit(1);
         return -1; /* PRESENTATION ERROR */
      }
         
      while (fscanf(fi2, "%d %d %d %d %c\n", &s1, &c1, &c2, &s2, &sm)==5)
      {
        if ((s1>=0) && (s1<MAXSTAVU) && (s2>=0) && (s2<MAXSTAVU) &&
           (c1>=-1) && (c2>=-1) && ((sm=='L') || (sm=='R'))
           && (stavy[s1][c1+1].stav == -1) && (s1<=n) && (s2<=n) && (c1<=m) && (c2<=m))
        {
          stavy[s1][c1+1].stav=s2;
          stavy[s1][c1+1].znak=c2;
          stavy[s1][c1+1].smer=sm;
        } else
        {
          printf("Presentation error!\n");
          exit(1);
          return -1; /* PRESENTATION ERROR */
        }
      }

      for (i=0; i<MAXP; i++) prom[i]=rand() % 4000;
  
      j=0;
      for (i=0; i<MAXP; i++)
      {
        int h;
    
        h=1;
        while (prom[i] >= 10*h) h*=10;
    
        while (h>0)
        {
          paska[j++]=(prom[i] / h) % 10;
          h/=10;
        }
        j++;
      }
  
      kroky=0;
      stav=0;
      pos=0;
      while (kroky<MAXKROKU)
      {
        kroky++;
    
        /* koncovy stav - nic nevede dal */
        if (stavy[stav][paska[pos]+1].stav==-1) break;
    
        s1=stav;
        sm=stavy[s1][paska[pos]+1].smer;
        stav=stavy[s1][paska[pos]+1].stav;
        paska[pos]=stavy[s1][paska[pos]+1].znak;
        if (sm=='L')
        {
          pos--;
          if (pos<0) pos+=MAXR;
        } else
        if (sm=='R')
        {
          pos++;
          if (pos>=MAXR) pos-=MAXR;
        }
      }
  
      {
        int v1, v2, k, h;
        v1=comp(st, strlen(st));
        h=v1;
        for (k=1; h>0; k++) h/=10;
        k+=10;
        
        v2=0;
        while ((paska[pos]!=-1)&&(k>0)) 
        {
          v2=v2*10+paska[pos++];
          k--;
        }
        if (v2!=v1)
        {
          printf("%d %d %d\n", v1, v2, kroky);
          printf("Wrong answer!\n");
          exit(2);
        }
      }
    }
  
  printf("OK.\n");
  return 0;
}


