#include <bits/stdc++.h>
using namespace std;
const long double EPS = 1e-11;
map<string, string> mp;
string Reguler(string str) {
  string res, key;
  int pos1, pos2, num, num2;
  pos1 = str.find_first_not_of('&');
  pos2 = str.find_last_not_of('*');
  num = pos2 - pos1 + 1;
  num2 = str.size() - 1 - pos2;
  key = str.substr(pos1, num);
  while (1) {
    if (key == "errtype" || mp.find(key) == mp.end()) {
      res = "errtype";
      break;
    } else if (key == "void") {
      if (pos1 > num2) {
        res = "errtype";
        break;
      } else {
        res = "void";
        for (int i = 0; i < num2 - pos1; i++) res += '*';
        break;
      }
    }
    str.clear();
    for (int i = 0; i < pos1; i++) str += '&';
    str += mp[key];
    for (int i = 0; i < num2; i++) str += '*';
    pos1 = str.find_first_not_of('&');
    pos2 = str.find_last_not_of('*');
    num = pos2 - pos1 + 1;
    num2 = str.size() - 1 - pos2;
    key = str.substr(pos1, num);
  }
  return res;
}
int main() {
  int n;
  string str, str1("typedef"), str2("typeof");
  string tmp1, tmp2;
  cin >> n;
  mp["void"] = "void";
  mp["errtype"] = "errtype";
  for (int i = 0; i < n; i++) {
    cin >> str;
    if (str == str1) {
      cin >> tmp1 >> tmp2;
      mp[tmp2] = Reguler(tmp1);
    } else {
      cin >> tmp1;
      cout << Reguler(tmp1) << endl;
    }
  }
  return 0;
}
