#include <bits/stdc++.h>
using namespace std;
int n, m;
map<string, int> mapa;
bitset<1001> tab[5007];
int lew[5007];
int pra[5007];
int op[5007];
bitset<1001> wynmin;
bitset<1001> wynmax;
int licz(int i) {
  int res = 0;
  for (int it = 1; it <= n; ++it) {
    if (op[it] == 1) {
      tab[it][i] = tab[lew[it]][i] ^ tab[pra[it]][i];
    } else if (op[it] == 2) {
      tab[it][i] = tab[lew[it]][i] | tab[pra[it]][i];
    } else if (op[it] == 3) {
      tab[it][i] = tab[lew[it]][i] & tab[pra[it]][i];
    }
    if (tab[it][i]) res++;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m;
  for (int it = 1; it <= n; ++it) {
    string nm;
    cin >> nm;
    mapa[nm] = it;
    string x;
    cin >> x;
    cin >> x;
    if (x[0] == '0' || x[0] == '1') {
      for (int i = 0; i < m; ++i) tab[it][i] = bool(x[i] - '0');
    } else {
      if (x[0] == '?')
        lew[it] = 0;
      else
        lew[it] = mapa[x];
      cin >> x;
      if (x[0] == 'X')
        op[it] = 1;
      else if (x[0] == 'O')
        op[it] = 2;
      else
        op[it] = 3;
      cin >> x;
      if (x[0] == '?')
        pra[it] = 0;
      else
        pra[it] = mapa[x];
    }
  }
  for (int i = 0; i < m; ++i) {
    int ile0, ile1;
    tab[0][i] = 0;
    ile0 = licz(i);
    tab[0][i] = 1;
    ile1 = licz(i);
    if (ile1 != ile0) {
      if (ile1 > ile0)
        wynmax[i] = 1;
      else
        wynmin[i] = 1;
    }
  }
  for (int i = 0; i < m; ++i) cout << wynmin[i];
  cout << endl;
  for (int i = 0; i < m; ++i) cout << wynmax[i];
  cout << endl;
  return 0;
}
