#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-6;
const int maxn = 5e3 + 5;
const int maxm = 1e6 + 5;
const int dx[] = {-1, 0, 1, 0, -1, -1, 1, 1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
inline int scan(int &a) { return scanf("%d", &a); }
inline int scan(int &a, int &b) { return scanf("%d%d", &a, &b); }
inline int scan(int &a, int &b, int &c) { return scanf("%d%d%d", &a, &b, &c); }
inline int scan(long long &a) { return scanf("%I64d", &a); }
inline int scan(long long &a, long long &b) {
  return scanf("%I64d%I64d", &a, &b);
}
inline int scan(long long &a, long long &b, long long &c) {
  return scanf("%I64d%I64d%I64d", &a, &b, &c);
}
inline int scan(double &a) { return scanf("%lf", &a); }
inline int scan(double &a, double &b) { return scanf("%lf%lf", &a, &b); }
inline int scan(double &a, double &b, double &c) {
  return scanf("%lf%lf%lf", &a, &b, &c);
}
inline int scan(char &a) { return scanf("%c", &a); }
inline int scan(char *a) { return scanf("%s", a); }
template <class T>
inline void mem(T &A, int x) {
  memset(A, x, sizeof(A));
}
template <class T0, class T1>
inline void mem(T0 &A0, T1 &A1, int x) {
  mem(A0, x), mem(A1, x);
}
template <class T0, class T1, class T2>
inline void mem(T0 &A0, T1 &A1, T2 &A2, int x) {
  mem(A0, x), mem(A1, x), mem(A2, x);
}
template <class T0, class T1, class T2, class T3>
inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, int x) {
  mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x);
}
template <class T0, class T1, class T2, class T3, class T4>
inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, int x) {
  mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5>
inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, int x) {
  mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x);
}
template <class T0, class T1, class T2, class T3, class T4, class T5, class T6>
inline void mem(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6, int x) {
  mem(A0, x), mem(A1, x), mem(A2, x), mem(A3, x), mem(A4, x), mem(A5, x),
      mem(A6, x);
}
template <class T>
inline T min(T a, T b, T c) {
  return min(min(a, b), c);
}
template <class T>
inline T max(T a, T b, T c) {
  return max(max(a, b), c);
}
template <class T>
inline T min(T a, T b, T c, T d) {
  return min(min(a, b), min(c, d));
}
template <class T>
inline T max(T a, T b, T c, T d) {
  return max(max(a, b), max(c, d));
}
template <class T>
inline T min(T a, T b, T c, T d, T e) {
  return min(min(min(a, b), min(c, d)), e);
}
template <class T>
inline T max(T a, T b, T c, T d, T e) {
  return max(max(max(a, b), max(c, d)), e);
}
template <class T>
inline void dbg(T a[], int n) {
  for (int i = 0; i < n; i++) cout << a[i] << (i == n - 1 ? "\n" : " ");
}
template <class T>
inline void dbg(T a) {
  cout << a << endl;
}
template <class T>
inline void dbg(T a[][maxn], int n, int m) {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cout << a[i][j] << (j == m - 1 ? "\n" : " ");
}
map<string, int> mp;
char s[1005];
int num[maxn][1005], op[maxn], v1[maxn], v2[maxn], res[2];
int val[maxn];
char mx[1005], mn[1005];
int main() {
  int n, m;
  scan(n, m);
  mp[string("?")] = n;
  for (int i = 0; i < n; i++) {
    scan(s);
    mp[s] = i;
    scan(s);
    scan(s);
    if (s[0] == '0' || s[0] == '1') {
      v1[i] = -1;
      for (int j = 0; j < m; j++) num[i][j] = s[j] - '0';
    } else {
      v1[i] = mp[s];
      scan(s);
      if (s[0] == 'A')
        op[i] = 0;
      else if (s[0] == 'O')
        op[i] = 1;
      else
        op[i] = 2;
      scan(s);
      v2[i] = mp[s];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < 2; j++) {
      res[j] = 0;
      val[n] = j;
      for (int k = 0; k < n; k++) {
        if (v1[k] == -1)
          val[k] = num[k][i];
        else {
          if (op[k] == 0)
            val[k] = val[v1[k]] & val[v2[k]];
          else if (op[k] == 1)
            val[k] = val[v1[k]] | val[v2[k]];
          else
            val[k] = val[v1[k]] ^ val[v2[k]];
        }
        res[j] += val[k];
      }
    }
    if (res[0] <= res[1])
      mx[i] = '0';
    else
      mx[i] = '1';
    if (res[0] >= res[1])
      mn[i] = '0';
    else
      mn[i] = '1';
  }
  dbg(mx);
  dbg(mn);
  return 0;
}
