#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5000;
int n, m;
string a[MAXN + 9], b[MAXN + 9], d[MAXN + 9], ans1, ans2;
int c[MAXN + 9], e[MAXN + 9], zzz[MAXN + 9];
map<string, int> mp;
int And(int x, int y) {
  if (x == 0 || y == 0) {
    return 0;
  }
  if (x == 1) {
    return y;
  }
  if (y == 1) {
    return x;
  }
  if (x == y) {
    return x;
  }
  return 0;
}
int Or(int x, int y) {
  if (x == 1 || y == 1) {
    return 1;
  }
  if (x == 0) {
    return y;
  }
  if (y == 0) {
    return x;
  }
  if (x == y) {
    return x;
  }
  return 1;
}
int Xor(int x, int y) {
  if (x == 0) {
    return y;
  }
  if (y == 0) {
    return x;
  }
  if (x == 1) {
    if (y < 2) {
      return 1 - y;
    } else {
      return 5 - y;
    }
  }
  if (y == 1) {
    if (x < 2) {
      return 1 - x;
    } else {
      return 5 - x;
    }
  }
  if (x == y) {
    return 0;
  }
  return 1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    string tmp;
    cin >> a[i] >> tmp >> tmp;
    mp[a[i]] = i;
    if (tmp[0] == '0' || tmp[0] == '1') {
      b[i] = tmp;
    } else {
      if (tmp == "?") {
        c[i] = 0;
      } else {
        c[i] = mp[tmp];
      }
      cin >> d[i] >> tmp;
      if (tmp == "?") {
        e[i] = 0;
      } else {
        e[i] = mp[tmp];
      }
    }
  }
  for (int j = 0; j < m; j++) {
    int cnt0 = 0, cnt1 = 0;
    zzz[0] = 3;
    for (int i = 1; i <= n; i++) {
      if (b[i] != "") {
        zzz[i] = b[i][j] - '0';
      } else {
        if (d[i] == "AND") {
          zzz[i] = And(zzz[c[i]], zzz[e[i]]);
          if (zzz[i] == 2) {
            cnt0++;
          } else if (zzz[i] == 3) {
            cnt1++;
          }
        }
        if (d[i] == "OR") {
          zzz[i] = Or(zzz[c[i]], zzz[e[i]]);
          if (zzz[i] == 2) {
            cnt0++;
          } else if (zzz[i] == 3) {
            cnt1++;
          }
        }
        if (d[i] == "XOR") {
          zzz[i] = Xor(zzz[c[i]], zzz[e[i]]);
          if (zzz[i] == 2) {
            cnt0++;
          } else if (zzz[i] == 3) {
            cnt1++;
          }
        }
      }
    }
    if (cnt0 < cnt1) {
      ans1.push_back('0');
      ans2.push_back('1');
    } else if (cnt0 > cnt1) {
      ans1.push_back('1');
      ans2.push_back('0');
    } else {
      ans1.push_back('0');
      ans2.push_back('0');
    }
  }
  cout << ans1 << "\n" << ans2 << "\n";
  return 0;
}
