#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using Pii = pair<int, int>;
using Vi = vector<int>;
void run();
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  cout << fixed << setprecision(18);
  run();
  return 0;
}
int uplg(int n) { return 32 - __builtin_clz(n); }
int uplg(ll n) { return 64 - __builtin_clzll(n); }
constexpr ll MOD = 15 * (1 << 27) + 1;
mt19937 rnd(int(time(0)));
struct Zp {
  ll first;
  Zp(ll a = 0) {
    if (a < 0)
      a = a % MOD + MOD;
    else if (a >= MOD * 2)
      a %= MOD;
    else if (a >= MOD)
      a -= MOD;
    first = a;
  }
  Zp operator+(Zp r) const { return first + r.first; }
  Zp operator-(Zp r) const { return first - r.first + MOD; }
  Zp operator*(Zp r) const { return first * r.first; }
  Zp operator/(Zp r) const { return first * r.inv().first; }
  Zp operator-() const { return MOD - first; }
  Zp inv() const { return pow(MOD - 2); }
  Zp pow(ll e) const {
    Zp t = 1, m = *this;
    while (e) {
      if (e & 1) t = t * m;
      e >>= 1;
      m = m * m;
    }
    return t;
  }
  Zp& operator+=(Zp r) { return *this = *this + r; }
  Zp& operator-=(Zp r) { return *this = *this - r; }
  Zp& operator*=(Zp r) { return *this = *this * r; }
  Zp& operator/=(Zp r) { return *this = *this / r; }
  void print() { cerr << first; }
  bool operator<(Zp r) const { return first < r.first; }
};
struct Info {
  string name;
  Zp a, b;
  char op{'x'};
  bool ok{0};
  ;
};
const Zp MULT = rnd();
map<Zp, Info> info;
map<string, Zp> vars;
vector<string> ans, names;
void dfs(Zp what, string name) {
  auto& dep = info[what];
  if (!dep.ok) {
    if (name.empty()) {
      name = names.back();
      names.pop_back();
    }
    dep.name = name;
    dep.ok = 1;
    dfs(dep.a, "");
    dfs(dep.b, "");
    ans.push_back(name + "=" + info[dep.a].name + dep.op + info[dep.b].name);
  } else if (name == "res") {
    ans.push_back("res=" + dep.name);
  }
}
void run() {
  int n;
  cin >> n;
  auto getVar = [&](const string& name) {
    if (vars.count(name)) {
      return vars[name];
    }
    Zp key = rnd();
    vars[name] = key;
    info[key].name = name;
    info[key].ok = 1;
    return key;
  };
  auto combine = [&](const string& a, const string& b, char op) {
    Zp va = getVar(a), vb = getVar(b);
    Zp ret = (va * MULT + vb) * MULT + op;
    info[ret].a = va;
    info[ret].b = vb;
    info[ret].op = op;
    return ret;
  };
  for (int i = (0); i < (n); i++) {
    string line;
    cin >> line;
    int sep = int(line.find("="));
    string dst = line.substr(0, sep), rest = line.substr(sep + 1);
    sep = -1;
    for (int j = (0); j < (int((rest).size())); j++) {
      char c = rest[j];
      if (c == '$' || c == '^' || c == '#' || c == '&') {
        sep = j;
        break;
      }
    }
    if (sep == -1) {
      if (dst != rest) vars[dst] = getVar(rest);
    } else {
      string a = rest.substr(0, sep), b = rest.substr(sep + 1);
      char op = rest[sep];
      vars[dst] = combine(a, b, op);
    }
  }
  for (int i = (0); i < (26); i++)
    for (int j = (0); j < (26); j++)
      for (int k = (0); k < (26); k++)
        for (int l = (0); l < (26); l++) {
          string name = {char(i + 'a'), char(j + 'a'), char(k + 'a'),
                         char(l + 'a')};
          if (vars.count(name)) continue;
          names.push_back(name);
          if (int((names).size()) >= 20000) {
            goto done;
          }
        }
done:
  if (vars.count("res") && info[vars["res"]].name != "res") {
    dfs(vars["res"], "res");
  }
  cout << int((ans).size()) << '\n';
  for (auto& a : (ans)) cout << a << '\n';
}
