#include <bits/stdc++.h>
using namespace std;
#pragma comment(linker, "/STACK:264777216")
vector<pair<string, string> > dp;
vector<pair<int, int> > put;
string rec(int st, string a, int left, int right) {
  if (a == "void") {
    if (left > right)
      return "errtype";
    else {
      right -= left;
      for (int i = 0; i < right; ++i) a += "*";
    }
    return a;
  }
  int nst = st;
  for (--nst; nst >= 0; --nst) {
    if (a == dp[nst].first) {
      break;
    }
  }
  if (nst < 0) return "errtype";
  string f = dp[nst].second, r;
  int L = 0, R = 0;
  for (int i = 0; i < f.size(); ++i) {
    if (f[i] != '&' && f[i] != '*') {
      r += f[i];
    }
  }
  for (int i = 0; i < f.size(); ++i) {
    if (f[i] != '&') break;
    ++L;
  }
  for (int i = f.size() - 1; i >= 0; --i) {
    if (f[i] != '*') break;
    ++R;
  }
  put.push_back(pair<int, int>(L, R));
  return rec(nst, r, left + L, right + R);
}
void Solve() {
  dp.clear();
  int n;
  cin >> n;
  char s[200];
  gets(s);
  for (int i = 0; i < n; ++i) {
    gets(s);
    string str;
    istringstream in(s);
    in >> str;
    if (str == "typedef") {
      string a, b;
      in >> a >> b;
      dp.push_back(pair<string, string>(b, a));
    } else {
      string a;
      in >> a;
      string f = a, r;
      for (int i = 0; i < f.size(); ++i) {
        if (f[i] != '&' && f[i] != '*') {
          r += f[i];
        }
      }
      int left = 0, right = 0;
      for (int i = 0; i < a.size(); ++i) {
        if (a[i] != '&') break;
        ++left;
      }
      for (int i = a.size() - 1; i >= 0; --i) {
        if (a[i] != '*') break;
        ++right;
      }
      put.clear();
      put.push_back(pair<int, int>(left, right));
      string pp = rec(dp.size(), r, left, right);
      if (pp == "errtype") {
        cout << "errtype" << endl;
      } else {
        int L = 0;
        int R = 0;
        bool is = true;
        for (int i = put.size() - 1; i >= 0; --i) {
          L += put[i].first;
          R += put[i].second;
          if (L > R) {
            is = false;
          }
        }
        if (is)
          cout << pp << endl;
        else
          cout << "errtype" << endl;
      }
    }
  }
}
int main() {
  Solve();
  return 0;
}
