#include <bits/stdc++.h>
using namespace std;
struct type {
  string t;
  int ast;
  int amp;
};
int main() {
  map<string, type> m;
  string op, t1, t2;
  int n, st, end, ast, amp;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> op >> t1;
    if (op == "typedef") {
      cin >> t2;
      ast = 0;
      amp = 0;
      for (int j = 0; j < t1.length(); j++) {
        if (t1[j] != '&') {
          st = j;
          break;
        }
      }
      for (int j = t1.length() - 1; j >= 0; j--) {
        if (t1[j] != '*') {
          end = j;
          break;
        }
      }
      ast = t1.length() - 1 - end;
      amp = st;
      t1 = t1.substr(st, end - st + 1);
      if (m.find(t1) != m.end()) {
        ast += m[t1].ast;
        amp += m[t1].amp;
        t1 = m[t1].t;
      } else if (t1 != "void")
        t1 = "errtype";
      if (amp > ast) t1 = "errtype";
      if (t1 == "errtype") {
        ast = 0;
        amp = 0;
      }
      m[t2].t = t1;
      m[t2].ast = ast;
      m[t2].amp = amp;
    } else {
      if (t1 == "void") {
        cout << t1 << endl;
        continue;
      }
      ast = 0;
      amp = 0;
      for (int j = 0; j < t1.length(); j++) {
        if (t1[j] != '&') {
          st = j;
          break;
        }
      }
      for (int j = t1.length() - 1; j >= 0; j--) {
        if (t1[j] != '*') {
          end = j;
          break;
        }
      }
      ast = t1.length() - 1 - end;
      amp = st;
      t1 = t1.substr(st, end - st + 1);
      if (m.find(t1) != m.end()) {
        ast += m[t1].ast;
        amp += m[t1].amp;
        t1 = m[t1].t;
        if (t1 == "errtype") {
          cout << "errtype\n";
        } else {
          int x = min(ast, amp);
          ast -= x;
          amp -= x;
          if (amp && ast == 0) {
            cout << "errtype\n";
          } else {
            for (int j = 0; j < amp; j++) cout << "&";
            cout << t1;
            for (int j = 0; j < ast; j++) cout << "*";
            cout << endl;
          }
        }
      } else {
        if (t1 == "void" && ast >= amp) {
          cout << "void";
          for (int j = 0; j < ast - amp; j++) cout << "*";
          cout << endl;
        } else
          cout << "errtype\n";
      }
    }
  }
  return 0;
}
