#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
template <typename T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T LCM(T a, T b) {
  return a * (b / gcd(a, b));
}
template <typename T>
T expo(T base, T e, T mod) {
  T res = 1;
  while (e > 0) {
    if (e & 1) res = res * base % mod;
    base = base * base % mod;
    e >>= 1;
  }
  return res;
}
template <typename T, typename second>
T expo(T b, second e) {
  if (e <= 1) return e == 0 ? 1 : b;
  return (e & 1) == 0 ? expo((b * b), e >> 1) : (b * expo((b * b), e >> 1));
}
template <typename T, typename second>
T modinv(T a, second mod) {
  return expo(a, mod - 2, mod);
}
template <class T, class second>
std::ostream &operator<<(std::ostream &os, const std::pair<T, second> &t) {
  os << "(" << t.first << ", " << t.second << ")";
  return os;
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &t) {
  os << "[";
  for (__typeof(t.begin()) it = t.begin(); it != t.end(); it++) {
    if (it != t.begin()) os << ", ";
    os << *it;
  }
  os << "]";
  return os;
}
const int MAXN = 5e3 + 3;
int n, m;
map<string, int> mapper;
vector<string> inp[MAXN];
string lines[MAXN];
int vars[MAXN][2];
string minim, maxim;
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  cin.ignore();
  for (int i = 0; i < n; i++) {
    getline(cin, lines[i]);
    stringstream ss(lines[i]);
    string word;
    while (getline(ss, word, ' ')) {
      inp[i].push_back(word);
    }
    mapper[inp[i][0]] = i;
  }
  for (int i = 0; i < m; i++) {
    memset(vars, 0, sizeof vars);
    string var_name, garb, a, op, b;
    int one = 0, zero = 0;
    for (int j = 0; j < n; j++) {
      if (inp[j].size() == 3) {
        vars[j][0] = vars[j][1] = inp[j][2][i] - '0';
      } else if (inp[j].size() == 5) {
        op = inp[j][3];
        if (inp[j][2] == "?" and inp[j][4] == "?") {
          if (op == "XOR") {
            vars[j][0] = vars[j][1] = 0;
          } else if (op == "AND") {
            vars[j][0] = 0;
            vars[j][1] = 1;
          } else if (op == "OR") {
            vars[j][0] = 0;
            vars[j][1] = 1;
          }
        } else if (inp[j][2] == "?" or inp[j][4] == "?") {
          if (inp[j][4] == "?") swap(inp[j][4], inp[j][2]);
          int idx = mapper[inp[j][4]];
          if (op == "XOR") {
            if (vars[idx][0] == 0) {
              vars[j][0] = 0;
            } else {
              vars[j][0] = 1;
            }
            if (vars[idx][1] == 0) {
              vars[j][1] = 1;
            } else {
              vars[j][1] = 0;
            }
          } else if (op == "AND") {
            vars[j][0] = 0;
            vars[j][1] = vars[idx][1];
          } else if (op == "OR") {
            if (vars[idx][0] == 0) {
              vars[j][0] = 0;
            } else
              vars[j][0] = 1;
            vars[j][1] = 1;
          }
        } else {
          int idx1 = mapper[inp[j][2]], idx2 = mapper[inp[j][4]];
          if (op == "XOR") {
            vars[j][0] = vars[idx1][0] ^ vars[idx2][0];
            vars[j][1] = vars[idx1][1] ^ vars[idx2][1];
          } else if (op == "AND") {
            vars[j][0] = vars[idx1][0] & vars[idx2][0];
            vars[j][1] = vars[idx1][1] & vars[idx2][1];
          } else {
            vars[j][0] = vars[idx1][0] | vars[idx2][0];
            vars[j][1] = vars[idx1][1] | vars[idx2][1];
          }
        }
      } else
        assert(false);
      zero += vars[j][0];
      one += vars[j][1];
    }
    if (one < zero) {
      minim.push_back('1');
      maxim.push_back('0');
    } else if (one == zero) {
      minim.push_back('0');
      maxim.push_back('0');
    } else {
      minim.push_back('0');
      maxim.push_back('1');
    }
  }
  cout << minim << '\n' << maxim << endl;
  return 0;
}
