#include <bits/stdc++.h>
using namespace std;
const int inf = 2009000999;
const double eps = 1e-3;
const int maxn = 5555;
const int base = 1073741789;
const long long basell = 1e18 + 3;
const long double PI = acos(-1.0);
string itosm(long long x) {
  string ans = "";
  while (x > 0) {
    ans += ((x % 10) + '0');
    x /= 10;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
long long stoim(string str) {
  long long ans = 0;
  long long k = 1;
  for (int i = str.length() - 1; i >= 0; i--) {
    ans += (str[i] - '0') * k;
    k *= 10;
  }
  return ans;
}
unordered_map<string, int> a;
int b[maxn];
pair<int, int> a2[maxn];
pair<pair<string, string>, int> p[maxn];
int main() {
  ios_base::sync_with_stdio(0);
  ;
  int n, m;
  cin >> n >> m;
  string s;
  cin >> s;
  a["?"] = -1;
  for (int i = 0; i < n; i++) {
    a[s] = i + 1;
    cin >> s;
    cin >> p[i].first.first;
    cin >> s;
    if (s == "AND") {
      p[i].second = 1;
    } else if (s == "XOR") {
      p[i].second = 3;
    } else if (s == "OR") {
      p[i].second = 2;
    } else {
      p[i].first.second = p[i].first.first;
      p[i].second = 1;
      continue;
    }
    cin >> p[i].first.second;
    cin >> s;
  }
  for (int i = 0; i < n; i++) {
    a2[i].first = a[p[i].first.first];
    a2[i].second = a[p[i].first.second];
  }
  string ans = "", ans1 = "";
  for (int i = 0; i < m; i++) {
    int cnt1 = 0, cnt2 = 0;
    b[-1] = 1;
    for (int j = 0; j < n; j++) {
      if (p[j].second == 1) {
        if (a2[j].first == 0) {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i] &&
                        p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1' && b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1' && b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second] && b[a2[j].first]);
          }
        }
      } else if (p[j].second == 2) {
        if (a2[j].first == 0) {
          if (a2[j].first == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i] ||
                        p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1' || b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1' || b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second] || b[a2[j].first]);
          }
        }
      } else {
        if (a2[j].first == 0) {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i]) ^
                       (p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1') ^ (b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1') ^ (b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second]) ^ (b[a2[j].first]);
          }
        }
      }
      cnt1 += b[j + 1];
    }
    b[-1] = 0;
    for (int j = 0; j < n; j++) {
      if (p[j].second == 1) {
        if (a2[j].first == 0) {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i] &&
                        p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1' && b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1' && b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second] && b[a2[j].first]);
          }
        }
      } else if (p[j].second == 2) {
        if (a2[j].first == 0) {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i] ||
                        p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1' || b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1' || b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second] || b[a2[j].first]);
          }
        }
      } else {
        if (a2[j].first == 0) {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == p[j].first.first[i]) ^
                       (p[j].first.second[i] == '1');
          } else {
            b[j + 1] = (p[j].first.first[i] == '1') ^ (b[a2[j].second]);
          }
        } else {
          if (a2[j].second == 0) {
            b[j + 1] = (p[j].first.second[i] == '1') ^ (b[a2[j].first]);
          } else {
            b[j + 1] = (b[a2[j].second]) ^ (b[a2[j].first]);
          }
        }
      }
      cnt2 += b[j + 1];
    }
    if (cnt1 > cnt2) {
      ans += "1";
      ans1 += "0";
    } else {
      ans += "0";
      if (cnt1 == cnt2)
        ans1 += "0";
      else
        ans1 += "1";
    }
  }
  cout << ans1 << "\n" << ans << "\n";
  return 0;
}
