#include <bits/stdc++.h>
using namespace std;
bool debug = 0;
int n, m, k;
int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0};
string direc = "RDLU";
long long ln, lk, lm;
void etp(bool f = 0) {
  puts(f ? "YES" : "NO");
  exit(0);
}
void addmod(int &x, int y, int mod = 1000000007) {
  x += y;
  if (x >= mod) x -= mod;
  assert(x >= 0 && x < mod);
}
void et() {
  puts("-1");
  exit(0);
}
char in[5105], out1[5105], out2[5105];
map<string, int> mp;
int sid;
int gid(string x) {
  if (mp.find(x) == mp.end()) mp[x] = ++sid;
  return mp[x];
}
struct mb {
  string val, ope;
  int ty, o1, o2, tv;
  int getp(int pos) {
    if (ty == 1) return val[pos];
    return tv;
  }
} q[5105];
int ck(int V, int pos) {
  q[1].tv = V;
  int ss = 0;
  for (int i = 2; i <= sid; i++) {
    if (q[i].ty == 1)
      ss += q[i].val[pos] - '0';
    else {
      int o1 = q[q[i].o1].getp(pos);
      int o2 = q[q[i].o2].getp(pos);
      int z = 0;
      if (q[i].ope[0] == 'X')
        z = o1 ^ o2;
      else if (q[i].ope[0] == 'A')
        z = o1 & o2;
      else
        z = o1 | o2;
      q[i].tv = z;
      ss += z;
    }
  }
  return ss;
}
void fmain(int tid) {
  scanf("%d%d", &n, &m);
  cin.getline(in, 5105);
  gid("?");
  for (int(i) = 1; (i) <= (int)(n); (i)++) {
    cin.getline(in, 5105);
    string name = "", t1 = "";
    int j = 0, N = strlen(in);
    while (in[j] != ' ') name.push_back(in[j++]);
    j += 4;
    while (j < N && in[j] != ' ') {
      t1 += in[j++];
    }
    int id = gid(name);
    if (j == N) {
      q[id].ty = 1;
      q[id].val = t1;
    } else {
      q[id].ty = 2;
      q[id].o1 = gid(t1);
      j++;
      t1 = "";
      while (j < N && in[j] != ' ') {
        t1 += in[j++];
      }
      q[id].ope = t1;
      j++;
      t1 = "";
      while (j < N && in[j] != ' ') {
        t1 += in[j++];
      }
      q[id].o2 = gid(t1);
    }
  }
  for (int(i) = 0; (i) < (int)(m); (i)++) {
    int x = ck(0, i);
    int y = ck(1, i);
    if (x == y) {
      out1[i] = '0';
      out2[i] = '0';
    } else if (x < y) {
      out1[i] = '0';
      out2[i] = '1';
    } else {
      out1[i] = '1';
      out2[i] = '0';
    }
  }
  puts(out1);
  puts(out2);
}
int main() {
  int t = 1;
  for (int(i) = 1; (i) <= (int)(t); (i)++) {
    fmain(i);
  }
  return 0;
}
