#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
long long dx[] = {-1, 0, 1, 0};
long long dy[] = {0, -1, 0, 1};
long long n, m;
map<string, long long> mp;
string s[5007];
string bin[5007];
long long a[5007], b[5007], op[5007];
long long z[5007][2];
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  mp["?"] = 0;
  z[0][0] = 0;
  z[0][1] = 1;
  for (long long i = 1; i <= n; i++) {
    cin >> s[i];
    mp[s[i]] = i;
    string t;
    cin >> t;
    cin >> t;
    if (t[0] == '0' || t[0] == '1') {
      bin[i] = t;
    } else {
      string A, B, o;
      A = t;
      cin >> o;
      cin >> B;
      bin[i] = "";
      a[i] = mp[A];
      b[i] = mp[B];
      if (o == "AND") op[i] = 0;
      if (o == "OR") op[i] = 1;
      if (o == "XOR") op[i] = 2;
    }
  }
  string mn = "", mx = "";
  for (long long i = 0; i < m; i++) {
    long long zero = 0, one = 0;
    for (long long j = 1; j <= n; j++) {
      if (bin[j].size() > 0) {
        z[j][0] = (bin[j][i] == '1');
        z[j][1] = (bin[j][i] == '1');
      } else {
        if (op[j] == 0) {
          for (long long x = 0; x < 2; x++) z[j][x] = (z[a[j]][x] & z[b[j]][x]);
        } else if (op[j] == 1) {
          for (long long x = 0; x < 2; x++) z[j][x] = (z[a[j]][x] | z[b[j]][x]);
        } else if (op[j] == 2) {
          for (long long x = 0; x < 2; x++) z[j][x] = (z[a[j]][x] ^ z[b[j]][x]);
        }
      }
      zero += z[j][0];
      one += z[j][1];
    }
    if (zero > one) {
      mx += "0";
      mn += '1';
    } else if (zero == one) {
      mn += '0';
      mx += '0';
    } else {
      mx += '1';
      mn += '0';
    }
  }
  cout << mn << "\n";
  cout << mx << "\n";
}
