#include <bits/stdc++.h>
const long long INF = (long long)1e18 + 123;
const int inf = (int)2e9 + 123;
const int mod = 1e9 + 7;
using namespace std;
map<string, pair<long long, long long> > f;
map<string, bool> u;
int n;
string s;
int main() {
  unsigned int FOR;
  asm("rdtsc" : "=A"(FOR));
  srand(FOR);
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    string t, a, b;
    cin >> t >> a;
    if (t == "typedef") {
      cin >> b;
      string s = "";
      long long x = 0, y = 0;
      for (int j = 0; j < a.size(); j++) {
        if (a[j] == '&')
          x++;
        else if (a[j] == '*')
          y++;
        else
          s += a[j];
      }
      if (s == "errorlevel") x = INF;
      if (s != "void") {
        if (!u[s]) {
          x = INF;
        } else {
          x += f[s].first;
          y += f[s].second;
        }
      }
      u[b] = 1;
      if (x > y) x = INF;
      f[b] = make_pair(x, y);
    } else {
      string s = "";
      long long x1 = 0, x2 = 0;
      for (int j = 0; j < a.size(); j++) {
        if (a[j] == '*') {
          x2++;
        } else if (a[j] == '&') {
          x1++;
        } else
          s += a[j];
      }
      pair<long long, long long> x = f[s];
      x.first += x1;
      x.second += x2;
      if (s == "void") {
        if (x1 > x2) {
          cout << "errtype";
        } else {
          cout << "void";
          while (x2 > x1) {
            cout << '*';
            x2--;
          }
        }
      } else if (!u[s] || x.first > x.second)
        cout << "errtype";
      else {
        x.second -= x.first;
        x.first = 0;
        while (x.first) {
          cout << '&';
          x.first--;
        }
        cout << "void";
        while (x.second) {
          cout << '*';
          x.second--;
        }
      }
      cout << endl;
    }
  }
  return 0;
}
