Problem: Let's analyze a program written on some strange programming language. The variables in this language have names consisting of 1 to 4 characters, and each character is a lowercase or an uppercase Latin letter, or a digit. There is an extra constraint that the first character should not be a digit.

There are four types of operations in the program, each denoted by one of the characters: $, ^, # or &.

Each line of the program has one of the following formats: 

  * <lvalue>=<rvalue>, where <lvalue> and <rvalue> are valid variable names; 
  * <lvalue>=<arg1><op><arg2>, where <lvalue>, <arg1> and <arg2> are valid variable names, and <op> is an operation character. 



The program is executed line-by-line, and the result of execution is stored in a variable having the name res. If res is never assigned in the program, then the result will be equal to the value of res before running the program.

Two programs are called equivalent if no matter which operations do characters $, ^, # and & denote (but, obviously, performing the same operation on the same arguments gives the same result) and which values do variables have before execution of program, the value of res after running the first program is equal to the value of res after running the second program (the programs are executed independently).

You are given a program consisting of n lines. Your task is to write a program consisting of minimum possible number of lines that is equivalent to the program you are given.

Input

The first line contains one integer n (1 ≤ n ≤ 1000) — the number of lines in the program.

Then n lines follow — the program itself. Each line corresponds to the format described in the statement and has no extra whitespaces.

Output

In the first line print k — the minimum number of lines in the equivalent program.

Then print k lines without any whitespaces — an equivalent program having exactly k lines, in the same format it is described in the statement.

Examples

Input


4
c=aa#bb
d12=c
res=c^d12
tmp=aa$c


Output


2
aaaa=aa#bb
res=aaaa^aaaa


Input


2
max=aaaa$bbbb
min=bbbb^aaaa


Output


0

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
std::array<char, 4> ops = {'$', '^', '#', '&'};
struct State {
  int op;
  int left;
  int right;
  bool operator<(const State &other) const {
    if (op != other.op) {
      return op < other.op;
    }
    if (left != other.left) {
      return left < other.left;
    }
    if (right != other.right) {
      return right < other.right;
    }
    return false;
  }
  bool operator==(const State &other) const {
    return op == other.op && left == other.left && right == other.right;
  }
  bool operator!=(const State &other) const { return !(*this == other); }
};
struct Solution {
  int n;
  std::vector<std::string> names;
  std::vector<int> varIdxStates;
  std::map<State, int> stateIdx;
  std::vector<State> states;
  std::vector<std::string> auxNames;
  std::vector<std::string> ans;
  std::map<int, int> stateToName;
  int getState(State &state) {
    auto it = stateIdx.find(state);
    if (it != stateIdx.end()) {
      return it->second;
    }
    int res = states.size();
    stateIdx[state] = res;
    states.push_back(state);
    return res;
  }
  int getName(std::string &name) {
    for (size_t i = 0; i < names.size(); i++) {
      if (names[i] == name) {
        return i;
      }
    }
    int res = names.size();
    names.push_back(name);
    State var{-1, res, 0};
    varIdxStates.push_back(getState(var));
    return res;
  }
  void genAuxNames() {
    auxNames.clear();
    std::set<std::string> namesSet(names.begin(), names.end());
    for (char s = 'a'; s <= 'z'; s++) {
      for (int i = 0; i < 1000; i++) {
        std::string name;
        name.push_back(s);
        int j = i;
        for (int k = 0; k < 3; k++) {
          name.push_back((j % 10) + '0');
          j /= 10;
        }
        std::reverse(name.begin() + 1, name.end());
        if (namesSet.count(name) == 0) {
          auxNames.push_back(name);
        }
      }
    }
  }
  std::string rec(int state, bool root) {
    State s = states[state];
    if (s.op == -1) {
      return names[s.left];
    }
    auto it = stateToName.find(state);
    if (it != stateToName.end()) {
      return auxNames[it->second];
    }
    std::string varLeft = rec(s.left, false);
    std::string varRight = rec(s.right, false);
    std::string our;
    int ourIdx;
    if (root) {
      our = "res";
      ourIdx = -1;
    } else {
      ourIdx = stateToName.size();
      our = auxNames[ourIdx];
    }
    std::string op(our);
    op.push_back('=');
    op.append(varLeft);
    op.push_back(ops[s.op]);
    op.append(varRight);
    ans.push_back(op);
    stateToName[state] = ourIdx;
    return our;
  }
  void run(std::istream &in, std::ostream &out) {
    in >> n;
    names.clear();
    varIdxStates.clear();
    stateIdx.clear();
    states.clear();
    for (int i = 0; i < n; i++) {
      std::string line;
      in >> line;
      int pos = line.find('=');
      std::string var = line.substr(0, pos);
      std::string expr = line.substr(pos + 1);
      int op = -1;
      std::string left, right;
      for (int j = 0; j < ops.size(); j++) {
        size_t opPos = expr.find(ops[j]);
        if (opPos != std::string::npos) {
          op = j;
          left = expr.substr(0, opPos);
          right = expr.substr(opPos + 1);
          break;
        }
      }
      int varIdx = getName(var);
      if (op == -1) {
        int otherIdx = getName(expr);
        varIdxStates[varIdx] = varIdxStates[otherIdx];
      } else {
        int leftIdx = getName(left);
        int rightIdx = getName(right);
        State state{op, varIdxStates[leftIdx], varIdxStates[rightIdx]};
        varIdxStates[varIdx] = getState(state);
      }
    }
    int idxRes = -1;
    for (int i = 0; i < names.size(); i++) {
      if (names[i] == "res") {
        idxRes = i;
        break;
      }
    }
    if (idxRes == -1) {
      out << "0\n";
      return;
    }
    int resState = varIdxStates[idxRes];
    genAuxNames();
    ans.clear();
    stateToName.clear();
    std::string name = rec(resState, true);
    if (name != "res") {
      out << ans.size() + 1 << "\n";
    } else {
      out << ans.size() << "\n";
    }
    for (int i = 0; i < ans.size(); i++) {
      out << ans[i] << "\n";
    }
    if (name != "res") {
      out << "res=" << name << "\n";
    }
  }
};
int main() {
  std::cin.sync_with_stdio(false);
  std::cin.tie(nullptr);
  Solution().run(std::cin, std::cout);
  return 0;
}