#include <bits/stdc++.h>
using namespace std;
template <typename A, typename B>
string to_string(pair<A, B> p);
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) { return '"' + s + '"'; }
string to_string(const char* s) { return to_string((string)s); }
string to_string(bool b) { return (b ? "true" : "false"); }
string to_string(vector<bool> v) {
  bool first = true;
  string res = "{";
  for (int i = 0; i < static_cast<int>(v.size()); i++) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}
template <size_t N>
string to_string(bitset<N> v) {
  string res = "";
  for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
  }
  return res;
}
template <typename A>
string to_string(A v) {
  bool first = true;
  string res = "{";
  for (const auto& x : v) {
    if (!first) {
      res += ", ";
    }
    first = false;
    res += to_string(x);
  }
  res += "}";
  return res;
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ")";
}
template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
  return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " +
         to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
const int M = 1e9 + 7;
vector<string> splits(string s) {
  int i = 0, n = s.size();
  vector<string> a;
  while (i < n) {
    while (i < n && s[i] == ' ') {
      ++i;
    }
    if (i >= n) {
      break;
    }
    string temp;
    while (i < n && s[i] != ' ') {
      temp.push_back(s[i]);
      ++i;
    }
    a.push_back(temp);
  }
  return a;
}
int m;
map<string, array<vector<int>, 2>> mp;
array<vector<int>, 2> merge(array<vector<int>, 2>& u, string& op,
                            array<vector<int>, 2>& v) {
  array<vector<int>, 2> a;
  a[0] = vector<int>(m);
  a[1] = vector<int>(m);
  if (op == "OR") {
    for (int j = 0; j < 2; ++j) {
      for (int i = 0; i < m; ++i) {
        a[j][i] = u[j][i] | v[j][i];
      }
    }
  } else if (op == "AND") {
    for (int j = 0; j < 2; ++j) {
      for (int i = 0; i < m; ++i) {
        a[j][i] = u[j][i] & v[j][i];
      }
    }
  } else {
    for (int j = 0; j < 2; ++j) {
      for (int i = 0; i < m; ++i) {
        a[j][i] = u[j][i] ^ v[j][i];
      }
    }
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n >> m;
  cin.ignore();
  array<vector<int>, 2> ta;
  ta[0] = vector<int>(m);
  ta[1] = vector<int>(m);
  for (int i = 0; i < m; ++i) {
    ta[1][i] = 1;
  }
  mp["?"] = ta;
  for (int _ = 0; _ < n; ++_) {
    string s;
    getline(cin, s, '\n');
    vector<string> a = splits(s);
    if ((int)a.size() == 5) {
      mp[a[0]] = merge(mp[a[2]], a[3], mp[a[4]]);
    } else {
      array<vector<int>, 2> ta;
      ta[0] = vector<int>(m);
      ta[1] = vector<int>(m);
      for (int i = 0; i < m; ++i) {
        if (a[2][i] == '1') {
          ta[0][i] = 1;
          ta[1][i] = 1;
        }
      }
      mp[a[0]] = ta;
    }
  }
  42;
  mp.erase("?");
  vector<int> cnt0(m), cnt1(m);
  for (auto it = mp.begin(); it != mp.end(); ++it) {
    for (int i = 0; i < m; ++i) {
      if (it->second[0][i]) {
        ++cnt0[i];
      }
      if (it->second[1][i]) {
        ++cnt1[i];
      }
    }
  }
  string ans1, ans2;
  for (int i = 0; i < m; ++i) {
    if (cnt0[i] < cnt1[i]) {
      ans1.push_back('0');
      ans2.push_back('1');
    } else if (cnt0[i] > cnt1[i]) {
      ans1.push_back('1');
      ans2.push_back('0');
    } else {
      ans1.push_back('0');
      ans2.push_back('0');
    }
  }
  cout << ans1 << "\n";
  cout << ans2 << "\n";
  return 0;
}
