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>
using namespace std;
template <typename T>
void read(T& x) {
  static char c;
  static int f;
  for (c = getchar(), f = 1; c < '0' || c > '9'; c = getchar())
    if (c == '-') f = -f;
  for (x = 0; c >= '0' && c <= '9'; c = getchar()) x = x * 10 + (c & 15);
  x *= f;
}
template <typename T>
void write(T x) {
  static char q[65];
  int cnt = 0;
  if (x < 0) putchar('-'), x = -x;
  q[++cnt] = x % 10, x /= 10;
  while (x) q[++cnt] = x % 10, x /= 10;
  while (cnt) putchar(q[cnt--] + '0');
}
const int maxn = 1005 * 3;
char s[maxn];
map<string, int> bo;
int val[maxn], tot, cnt, ls[maxn], rs[maxn], TS[maxn][maxn];
string st[maxn];
int solve(int x, int y) {
  if (~TS[x][y]) return TS[x][y];
  if (!ls[x] || !ls[y]) return TS[x][y] = (val[x] == val[y]);
  return TS[x][y] =
             (val[x] == val[y] && solve(ls[x], ls[y]) && solve(rs[x], rs[y]));
}
int toInt(char c) { return c == '$' ? 1 : c == '^' ? 2 : c == '#' ? 3 : 4; }
char toChar(int c) { return c == 1 ? '$' : c == 2 ? '^' : c == 3 ? '#' : '&'; }
string tostring(int number) {
  string re = "";
  while (number) re = (char)(number % 10 + '0') + re, number /= 10;
  return re;
}
int sk[maxn], tp, all, as;
string cg[maxn], ans[maxn];
void dfs(int u, bool isRoot = false) {
  if (!ls[u]) return cg[u] = st[u], void();
  if (cg[u] != "") return cg[u], void();
  for (int i = 1; i <= tp; ++i) {
    if (TS[u][sk[i]]) {
      return cg[u] = cg[sk[i]], void();
    }
  }
  dfs(ls[u]);
  dfs(rs[u]);
  if (isRoot)
    cg[u] = "res";
  else
    cg[u] = "a" + tostring(++all);
  ans[++as] = cg[u] + "=" + cg[ls[u]] + toChar(val[u]) + cg[rs[u]];
  sk[++tp] = u;
}
int main() {
  cnt = 4;
  int n;
  read(n);
  for (int i = 1; i <= n; ++i) {
    scanf("%s", s);
    int p0 = -1, p1 = -1, len = strlen(s);
    for (int j = 0; j < len; ++j) {
      if (s[j] == '=') p0 = j;
      if (s[j] == '$' || s[j] == '^' || s[j] == '#' || s[j] == '&') p1 = j;
    }
    if (~p1) {
      string a = "", b = "", c = "";
      for (int j = 0; j < p0; ++j) c = c + s[j];
      for (int j = p0 + 1; j < p1; ++j) a = a + s[j];
      for (int j = p1 + 1; j < len; ++j) b = b + s[j];
      if (bo.find(a) == bo.end()) val[bo[a] = ++tot] = ++cnt, st[tot] = a;
      if (bo.find(b) == bo.end()) val[bo[b] = ++tot] = ++cnt, st[tot] = b;
      ++tot;
      ls[tot] = bo[a], rs[tot] = bo[b];
      val[bo[c] = tot] = toInt(s[p1]);
    } else {
      string a = "", b = "";
      for (int j = 0; j < p0; ++j) a = a + s[j];
      for (int j = p0 + 1; j < len; ++j) b = b + s[j];
      if (a == b) continue;
      if (bo.find(b) == bo.end()) val[bo[b] = ++tot] = ++cnt, st[tot] = b;
      bo[a] = bo[b];
    }
  }
  string res = "res";
  if (bo.find(res) == bo.end())
    puts("0");
  else {
    memset(TS, -1, sizeof TS);
    for (int i = 1; i <= tot; ++i)
      for (int j = 1; j <= tot; ++j) solve(i, j);
    int rt = bo[res];
    dfs(rt, 1);
    if (cg[rt] != res) ans[++as] = res + "=" + cg[rt];
    write(as), putchar('\n');
    for (int i = 1; i <= as; ++i) cout << ans[i] << endl;
  }
  return 0;
}