#include <bits/stdc++.h>
using namespace std;
const int N = 5010, M = 1010;
int n, m, ope[N][2], dig[N][M], mini[M], maxi[M], a[N];
string name[N], kind[N];
unordered_map<string, int> s2i;
void calc_dig(int d) {
  pair<int, int> mi(INT_MAX, INT_MAX), ma(INT_MAX, INT_MAX);
  for (int(x) = (0); (x) < (int)(2); ++(x)) {
    int sm = 0;
    a[n] = x;
    for (int(i) = (0); (i) < (int)(n); ++(i)) {
      if (kind[i] == "") {
        a[i] = dig[i][d];
      }
      int u = a[ope[i][0]], v = a[ope[i][1]];
      if (kind[i] == "AND") a[i] = u & v;
      if (kind[i] == "OR") a[i] = u | v;
      if (kind[i] == "XOR") a[i] = u ^ v;
      sm += a[i];
    }
    (mi) = min((mi), (make_pair(sm, x)));
    (ma) = min((ma), (make_pair(-sm, x)));
  }
  mini[d] = mi.second;
  maxi[d] = ma.second;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  name[n] = "?";
  s2i["?"] = n;
  for (int(i) = (0); (i) < (int)(n); ++(i)) {
    cin >> name[i];
    s2i[name[i]] = i;
    string tmp;
    cin >> tmp >> tmp;
    if (isdigit(tmp[0])) {
      for (int(j) = (0); (j) < (int)(m); ++(j)) dig[i][j] = tmp[j] - '0';
    } else {
      ope[i][0] = s2i[tmp];
      cin >> kind[i] >> tmp;
      ope[i][1] = s2i[tmp];
    }
  }
  for (int(i) = (0); (i) < (int)(m); ++(i)) calc_dig(i);
  for (int(i) = (0); (i) < (int)(m); ++(i)) cout << mini[i];
  cout << endl;
  for (int(i) = (0); (i) < (int)(m); ++(i)) cout << maxi[i];
  cout << endl;
}
