#include <bits/stdc++.h>
using namespace std;
int n, m, cont[3][1001][2], tme, x, tme2, det[5005], val[5005][1005][2],
    cnt[1005][2], cnt2;
string a, b, c, d, tmp, mx, mn;
map<string, int> ma;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m;
  for (int o = 0; o < n; ++o) {
    cin >> a;
    cin >> tmp;
    cin >> b;
    ma[a] = tme2++;
    if (b[0] == '1' || b[0] == '0') {
      int cur = 0;
      int z = tme2 - 1, x;
      for (int j = 0; j < m; ++j) {
        x = b[j] - '0';
        val[z][j][0] = val[z][j][1] = x;
        cnt[j][0] += x;
        cnt[j][1] += x;
      }
    } else {
      cin >> c >> d;
      if (d == "?" && b == "?") {
        if (c != "XOR") ++cnt2;
        continue;
      } else if (d == "?" || b == "?") {
        if (b == "?") swap(b, d);
        int x = ma[b], z = tme2 - 1;
        for (int i = 0; i < m; ++i) {
          if (c == "XOR") {
            val[z][i][0] = val[x][i][0] ^ 0;
            val[z][i][1] = val[x][i][1] ^ 1;
          } else if (c == "AND") {
            val[z][i][0] = 0;
            val[z][i][1] = val[x][i][1];
          } else {
            val[z][i][0] = val[x][i][0];
            val[z][i][1] = 1;
          }
          cnt[i][1] += val[z][i][1];
          cnt[i][0] += val[z][i][0];
        }
      } else {
        int x = ma[b], y = ma[d], z = tme2 - 1;
        for (int i = 0; i < m; ++i) {
          if (c == "XOR") {
            val[z][i][0] = val[x][i][0] ^ val[y][i][0];
            val[z][i][1] = val[x][i][1] ^ val[y][i][1];
          } else if (c == "AND") {
            val[z][i][0] = val[y][i][0] & val[x][i][0];
            val[z][i][1] = val[y][i][1] & val[x][i][1];
          } else {
            val[z][i][0] = val[x][i][0] | val[y][i][0];
            val[z][i][1] = val[x][i][1] | val[y][i][1];
          }
          cnt[i][1] += val[z][i][1];
          cnt[i][0] += val[z][i][0];
        }
      }
    }
  }
  for (int i = 0; i < m; ++i) {
    if (cnt[i][1] + cnt2 < cnt[i][0])
      mn += "1";
    else
      mn += "0";
  }
  for (int i = 0; i < m; ++i) {
    if (cnt[i][0] >= cnt[i][1] + cnt2)
      mx += "0";
    else
      mx += "1";
  }
  cout << mn << '\n' << mx << '\n';
}
