#include "testlib.h"
#include <string>
#include <vector>
#include <iostream>

using namespace std;

void make( vector <string> &s, InStream & ans, int tn, TResult wa, TResult pe )
{
  vector <int> a, b, c;
  int n = s.size() - 1, m = s[0].size();

  for (int i = 0; i < tn; i++)
  {
    int x = ans.readInt(), y = ans.readInt(), z = ans.readInt();
    if (x < 1 || x > n || y < 1 || y > n)
      quit(pe, "i or j is not valid");
    if (z < 1 || z > m)
      quit(pe, "k is not valid");
    a.push_back(x);
    b.push_back(y);
    c.push_back(z);
  }
  for (int i = 0; i < tn; i++)
    swap(s[a[i] - 1][c[i] - 1], s[b[i] - 1][c[i] - 1]);
  bool ok = false;
  for (int i = 0; i < n; i++)
    if (s[i] == s[n])
    {
      ok = true;
      break;
    }
  if (!ok && tn != -1)
    quit(wa, "this combination doesn't open the safe");
}

string str( int x )
{
  char buff[500];
  sprintf(buff, "%d", x);
  return buff;
}

int main( int argc, char **argv )
{
  registerTestlibCmd(argc, argv);
  int ja = ans.readInt();
  int pa = ouf.readInt();

  vector <string> a, b;
  int n = inf.readInt();
  for (int i = 0; i <= n; i++)
    a.push_back(inf.readWord());

  b = a;
  make(a, ans, ja, _fail, _fail);
  make(b, ouf, pa, _wa, _pe);

  if (ja == pa)
    quit(_ok, "ok " + str(ja));

  if ((pa != -1 && pa < ja) || (ja == -1 && pa != -1))
    quit(_fail, "contestant's solution is better");
  quit(_wa, "expected " + str(ja) + " found " + str(pa));
          
  return -1;
}
