#include <bits/stdc++.h>
struct Vertex {
  bool init;
  std::string s;
  Vertex *lhs, *rhs;
  bool calced;
  std::string ssa;
  Vertex() : init(false), lhs(nullptr), rhs(nullptr), calced(false), ssa("") {}
  friend bool operator==(const Vertex &a, const Vertex &b) {
    return (a.init == b.init) && (a.s == b.s) && (a.lhs == b.lhs) &&
           (a.rhs == b.rhs);
  }
};
std::set<std::string> initS;
std::string nextSSA = "acaa";
std::string get_ssa() {
  while (true) {
    nextSSA[0]++;
    for (uint32_t i = 0; i < nextSSA.size(); i++) {
      if (nextSSA[i] > 'z') {
        nextSSA[i + 1]++;
        nextSSA[i] = 'a';
      }
    }
    if (initS.find(nextSSA) == initS.end()) {
      break;
    }
  }
  return nextSSA;
}
const uint32_t MAX_V = 5000;
Vertex vertMem[MAX_V];
Vertex *vertPtr = vertMem;
std::map<std::string, Vertex *> curV;
Vertex *get_vertex() { return vertPtr++; }
Vertex *get_vertex(const std::string &s) {
  auto it = curV.find(s);
  if (it == curV.end()) {
    Vertex *ans = get_vertex();
    ans->ssa = s;
    ans->init = true;
    initS.insert(s);
    std::cerr << "Vertex " << ans - vertMem << " initial " << s << "\n";
    curV[s] = ans;
    return ans;
  } else {
    return it->second;
  }
}
std::vector<std::string> rawP;
void input() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  uint32_t n;
  std::cin >> n;
  rawP.resize(n);
  for (std::string &s : rawP) {
    std::cin >> s;
  }
}
bool is_opr(char c) {
  return (c == '$') || (c == '^') || (c == '#') || (c == '&') || (c == '=');
}
std::vector<std::string> tokenize(const std::string &s) {
  std::vector<std::string> ans;
  std::string curS = "";
  bool op = false;
  for (uint32_t i = 0; i < s.size(); i++) {
    bool curOp = is_opr(s[i]);
    if (op == curOp) {
      curS += s[i];
    } else {
      ans.push_back(curS);
      op = curOp;
      curS = s[i];
    }
  }
  ans.push_back(curS);
  return ans;
}
void build_dag() {
  for (const std::string &s : rawP) {
    auto l = tokenize(s);
    if (l.size() == 3) {
      Vertex *v = get_vertex(l[2]);
      curV[l[0]] = v;
    } else {
      Vertex *v = get_vertex();
      v->s = l[3];
      v->lhs = get_vertex(l[2]);
      v->rhs = get_vertex(l[4]);
      for (Vertex *u = vertMem; u != vertPtr; u++) {
        if ((*u) == (*v)) {
          if (v == u) {
            std::cerr << "vertex " << u - vertMem << ": ";
            std::cerr << "lhs " << u->lhs - vertMem;
            std::cerr << " opr " << u->s;
            std::cerr << " rhs " << u->rhs - vertMem << "\n";
          }
          curV[l[0]] = u;
          break;
        }
      }
    }
  }
}
std::vector<std::string> optP;
void build_program(Vertex *v) {
  if ((v->calced) || (v->init)) {
    return;
  } else {
    build_program(v->lhs);
    build_program(v->rhs);
    if (v->ssa.empty()) {
      v->ssa = get_ssa();
    }
    optP.push_back(v->ssa + "=" + v->lhs->ssa + v->s + v->rhs->ssa);
    v->calced = true;
  }
}
int main() {
  input();
  build_dag();
  auto it = curV.find("res");
  if (it != curV.end()) {
    if ((it->second->init) && (it->second->ssa != "res")) {
      optP.push_back("res=" + it->second->ssa);
    } else {
      it->second->ssa = "res";
      build_program(it->second);
    }
  }
  std::cout << optP.size() << "\n";
  for (const std::string &s : optP) {
    std::cout << s << "\n";
  }
}
