#include <bits/stdc++.h>
using namespace std;
map<string, string> conv;
int N;
int anper(string str) {
  int cnt = 0;
  for (auto &x : str) cnt += (x == '&');
  return cnt;
}
int ast(string str) {
  int cnt = 0;
  for (auto &x : str) cnt += (x == '*');
  return cnt;
}
int voidd(string a) {
  string res = "";
  for (auto &x : a) {
    if (x != '&' && x != '*') {
      res += x;
    }
  }
  return res == "void";
}
int errtype(string a) {
  string res = "";
  for (auto &x : a) {
    if (x != '&' && x != '*') {
      res += x;
    }
  }
  return res == "errtype";
}
string limpia(string a) {
  string n = "";
  for (auto &x : a) {
    if (x != '*' && x != '&') {
      n += x;
    }
  }
  return n;
}
string chida(string a) {
  int as = ast(a);
  int anp = anper(a);
  a = limpia(a);
  if (as < anp) {
    string res = "errtype";
    return res;
  }
  if (as == anp) {
    as = 0;
    anp = 0;
  }
  as -= anp;
  anp = 0;
  string res = "";
  while (anp > 0 && anp) {
    res += '&';
    anp--;
  }
  res += a;
  while (as >= 0 && as) {
    res += '*';
    as--;
  }
  return res;
}
string getRes(string a) {
  if (voidd(a)) {
    return chida(a);
  }
  if (errtype(a)) {
    return limpia(a);
  }
  if (conv.find(limpia(a)) == conv.end()) {
    return "errtype";
  }
  int as = ast(a);
  int anp = anper(a);
  string n = getRes(conv[limpia(a)]);
  int as2 = ast(n);
  int anp2 = anper(n);
  n = limpia(n);
  int anp3 = anp + anp2;
  int as3 = as + as2;
  if (as3 < anp3) {
    string res = "errtype";
    return res;
  }
  if (as3 == anp3) {
    as3 = 0;
    anp3 = 0;
  }
  as3 -= anp3;
  anp3 = 0;
  string res = "";
  while (anp3 > 0 && anp3) {
    res += '&';
    anp3--;
  }
  res += n;
  while (as3 >= 0 && as3) {
    res += '*';
    as3--;
  }
  return res;
}
string change(string a) {
  string r = getRes(a);
  return r;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N; i++) {
    string str, a, b;
    cin >> str;
    if (str == "typedef") {
      cin >> a >> b;
      conv[b] = change(a);
    } else {
      cin >> a;
      string res = getRes(a);
      if (errtype(res)) res = limpia(res);
      cout << res << '\n';
      conv[a] = res;
    }
  }
  return 0;
}
