#include <bits/stdc++.h>
using namespace std;
const int N = 100500;
int n, m;
string s[N], req[N][4], per[N];
pair<string, string> zp[N];
int b[N], link[N][2];
int calc(int pos, int to) {
  b[0] = to;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    string nw = "";
    if (s[i][int(s[i].size()) - 1] - '0' <= 1) {
      b[i] = zp[i].second[pos - 1] - '0';
    } else {
      int x = b[link[i][0]], y = b[link[i][1]];
      int res = 0;
      if (req[i][2] == "AND") res = x & y;
      if (req[i][2] == "XOR") res = x ^ y;
      if (req[i][2] == "OR") res = x | y;
      b[i] = res;
    }
    if (b[i] == 1) cnt++;
  }
  return cnt;
}
pair<char, char> go(int pos) {
  char mns = '0';
  int zer = calc(pos, 0), one = calc(pos, 1);
  if (zer > one) mns = '1';
  char mxs = '0';
  if (zer < one) mxs = '1';
  return make_pair(mns, mxs);
}
int main() {
  srand(time(NULL));
  scanf("%d%d\n", &n, &m);
  for (int i = 1; i <= n; i++) {
    char ch;
    while (scanf("%c", &ch) == 1) {
      if (ch == '\n') break;
      s[i] += ch;
    }
  }
  per[0] = "?";
  for (int i = 1; i <= n; i++) {
    if (s[i][int(s[i].size()) - 1] - '0' <= 1) {
      string word = "", num = "";
      for (int j = 0; j < int(s[i].size()); j++) {
        if (s[i][j] >= 'a' && s[i][j] <= 'z') {
          word += s[i][j];
        } else if (s[i][j] != ' ' && s[i][j] - '0' <= 1)
          num += s[i][j];
      }
      zp[i] = make_pair(word, num);
      per[i] = word;
    } else {
      string nws[4];
      nws[0] = nws[1] = nws[2] = nws[3] = "";
      int l = 0;
      for (int j = 0; j < int(s[i].size()); j++) {
        if (s[i][j] == ':') l++;
        if (s[i][j] >= 'A' && s[i][j] <= 'Z') {
          l++;
          while (s[i][j] >= 'A' && s[i][j] <= 'Z') {
            nws[l] += s[i][j];
            j++;
          }
          l++;
          j--;
          continue;
        }
        if (s[i][j] == '?' || (s[i][j] >= 'a' && s[i][j] <= 'z'))
          nws[l] += s[i][j];
      }
      for (int j = 0; j < 4; j++) req[i][j] = nws[j];
      for (int j = 0; j < i; j++) {
        if (req[i][1] == per[j]) link[i][0] = j;
        if (req[i][3] == per[j]) link[i][1] = j;
      }
      per[i] = nws[0];
    }
  }
  string mx = "", mn = "";
  for (int i = 1; i <= m; i++) {
    pair<char, char> ans = go(i);
    mn += ans.first;
    mx += ans.second;
  }
  cout << mn << "\n" << mx;
  return 0;
}
