/**
 * Standard checker: Template. Output and answer should be in the same format.
 */

#ifdef BURUNDUK
#  include "my_testlib.h"
#else
#  include "testlib.h"
#endif

#include <cstring>
#include <string>
#include <algorithm>
#include <numeric>

using namespace std;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)

const int VN = (int)1e5;

string s;
int sn, res_k, is[26], u[26];

int k, len, vn = 1, Next[VN][2];
char label[VN];

bool good( char c )
{
  return 'a' <= c && c <= 'z';
}

void readInput( InStream &in )
{
  s = in.readLine();
  sn = s.length();
  forn(i, sn)
  {
    ensure(good(s[i]));
    is[s[i] - 'a'] = 1;
  }
  res_k = accumulate(is, is + 26, 0);
}

int checkOutput( InStream &out )
{
  int k = out.readInt();
  int len = out.readInt();
  //fprintf(stderr, "%d %d\n", k, len);
  if (k != res_k)
    out.quitf(_wa, "number of different symbols is %d, not %d", res_k, k);
  out.readEoln();

  vn = 1;
  memset(Next, 0, sizeof(Next));
  memset(label, 0, sizeof(label));
  memset(u, 0, sizeof(u));
  forn(_, k)
  {
    char ch = out.readChar();
    if (!good(ch))
      out.quitf(_pe, "alpha expected symbol with code %d found", ch);
    if (++u[ch - 'a'] > 1)
      out.quitf(_wa, "'%c' meets twice", ch);
    out.readChar(':');
    out.readSpace();

    string s = out.readLine();
    //fprintf(stderr, "s = %s\n", s.c_str());

    int v = 0, i = 0, len = s.length();
    if (len == 0)
      out.quitf(_pe, "empty code is not allowed");
    while (i < len)
    {           
      if (s[i] != '0' && s[i] != '1')
        out.quitf(_pe, "symbol with code %d is not allowed in code", s[i]);
      int &r = Next[v][s[i++] - '0'];
      if (label[v])
        quitf(_wa, "prefixes detected!");
      if (!r)
      {
        if (vn == VN)
          out.quitf(_wa, "too long codes smth is wrong");
        r = vn++;
      }
      v = r;
    }
    if (label[v])
      quitf(_wa, "prefixes detected!");
    label[v] = ch;
  }
  string code = out.readLine();
  int pos = 0, code_len = code.length();
  forn(i, sn)
  {
    int v = 0;
    while (!label[v])
    {
      if (pos == code_len)
        quitf(_wa, "unexpected end of encoding");
      if (code[pos] != '0' && code[pos] != '1')
        quitf(_pe, "unexpected symbol with code %d in encoding", code[pos]);
      v = Next[v][code[pos++] - '0'];
    }
    if (label[v] != s[i])
      quitf(_wa, "position #%d: %c != %c", i + 1, label[v], s[i]);
  }
  if (pos < code_len)
    quitf(_wa, "some extra symbols in encoding");
  return len;
}

int main(int argc, char *argv[])
{
  registerTestlibCmd(argc, argv);

  readInput(inf);

  int ja = checkOutput(ans);
  int ca = checkOutput(ouf);

  if (ca != ja)
    quitf(ca > ja ? _wa : _fail, "Contestant says %d, jury say %d", ca, ja);
  quitf(_ok, "OK (text = %d, compressed = %d)", sn, ja);
  return 0;
}
