#include <bits/stdc++.h>
using namespace std;
map<string, string> m;
map<string, string>::iterator it;
int main() {
  int n, a, p;
  cin >> n;
  string s, l, r, tmp, tp;
  m["void"] = "void";
  m["errtype"] = "errtype";
  for (int i = 0; i < n; i++) {
    cin >> s >> l;
    if (s == "typedef") {
      cin >> r;
      a = p = 0;
      for (int i = 0; i < l.length(); i++)
        if (l[i] == '&')
          a++;
        else if (l[i] == '*')
          p++;
      int k = p - a;
      tp = l.substr(a, l.length() - p - a);
      it = m.find(tp);
      if (it == m.end())
        tmp = "errtype";
      else {
        if (it->second == "errtype")
          tmp = it->second;
        else {
          tmp = l.substr(0, a) + it->second + l.substr(l.length() - p);
          a = p = 0;
          for (int i = 0; i < tmp.length(); i++)
            if (tmp[i] == '&')
              a++;
            else if (tmp[i] == '*')
              p++;
          if (a - p > 0)
            tmp = "errtype";
          else {
            tmp = tmp.substr(a, tmp.length() - a - p);
            for (int i = 0; i < p - a; i++) tmp += '*';
          }
        }
      }
      m[r] = tmp;
    } else {
      a = p = 0;
      for (int i = 0; i < l.length(); i++)
        if (l[i] == '&')
          a++;
        else if (l[i] == '*')
          p++;
      int k = p - a;
      tp = l.substr(a, l.length() - p - a);
      it = m.find(tp);
      if (it == m.end())
        tmp = "errtype";
      else {
        if (it->second == "errtype")
          tmp = it->second;
        else {
          tmp = l.substr(0, a) + it->second + l.substr(l.length() - p);
          a = p = 0;
          for (int i = 0; i < tmp.length(); i++)
            if (tmp[i] == '&')
              a++;
            else if (tmp[i] == '*')
              p++;
          if (a - p > 0)
            tmp = "errtype";
          else {
            tmp = tmp.substr(a, tmp.length() - a - p);
            for (int i = 0; i < p - a; i++) tmp += '*';
          }
        }
      }
      cout << tmp << endl;
    }
  }
}
