#include <bits/stdc++.h>
using namespace std;
struct nodeS {
  int l, r;
  int x;
  nodeS* nodeL;
  nodeS* nodeR;
};
nodeS* version[100100];
nodeS* buildSum(int l, int r) {
  nodeS* now = new nodeS();
  now->l = l;
  now->r = r;
  now->x = 0;
  now->nodeL = now->nodeR = NULL;
  return now;
}
void pushLazySum(nodeS* now) {
  if (now->l == now->r) {
    return;
  } else {
    if (now->nodeL == NULL) {
      int m = (now->l + now->r) / 2;
      now->nodeL = buildSum(now->l, m);
      now->nodeR = buildSum(m + 1, now->r);
      now->nodeL->x = now->x;
      now->nodeR->x = now->x;
    }
  }
}
nodeS* updateSum(nodeS* refNode, int q, int dif) {
  pushLazySum(refNode);
  nodeS* now = new nodeS();
  now->l = refNode->l;
  now->r = refNode->r;
  now->x = refNode->x + dif;
  if (now->l == now->r) {
  } else {
    if (q <= refNode->nodeL->r) {
      now->nodeL = updateSum(refNode->nodeL, q, dif);
      now->nodeR = refNode->nodeR;
    } else {
      now->nodeL = refNode->nodeL;
      now->nodeR = updateSum(refNode->nodeR, q, dif);
    }
    now->x = now->nodeL->x + now->nodeR->x;
  }
  return now;
}
int querySum(nodeS* now, int q) {
  pushLazySum(now);
  if (now->l == now->r) {
    return now->x;
  }
  if (now->nodeL->r < q) {
    return now->nodeL->x + querySum(now->nodeR, q);
  } else {
    return querySum(now->nodeL, q);
  }
}
char s[100];
nodeS* day_version[100100];
map<string, int> subjectToId;
nodeS* buildDay(int l, int r) {
  nodeS* now = new nodeS();
  now->l = l;
  now->r = r;
  now->x = 1999999999;
  if (l == r) {
    now->nodeL = now->nodeR = NULL;
  } else {
    int m = (l + r) / 2;
    now->nodeL = buildDay(l, m);
    now->nodeR = buildDay(m + 1, r);
  }
  return now;
}
nodeS* updatePr(nodeS* refNode, int id, int pr) {
  nodeS* now = new nodeS();
  now->l = refNode->l;
  now->r = refNode->r;
  now->x = refNode->x;
  now->nodeL = now->nodeR = NULL;
  if (now->l == now->r) {
    now->x = pr;
  } else {
    if (id <= refNode->nodeL->r) {
      now->nodeL = updatePr(refNode->nodeL, id, pr);
      now->nodeR = refNode->nodeR;
    } else {
      now->nodeL = refNode->nodeL;
      now->nodeR = updatePr(refNode->nodeR, id, pr);
    }
  }
  return now;
}
int getPr(nodeS* now, int id) {
  if (now->l == now->r) return now->x;
  if (id <= now->nodeL->r) return getPr(now->nodeL, id);
  return getPr(now->nodeR, id);
}
int main() {
  int n;
  scanf("%d", &n);
  day_version[0] = buildDay(0, 100100);
  version[0] = buildSum(0, 1000000100);
  for (int i = 1; i <= n; i++) {
    version[i] = version[i - 1];
    day_version[i] = day_version[i - 1];
    scanf("%s", s);
    string ss = s;
    if (ss == "undo") {
      int d;
      scanf("%d", &d);
      version[i] = version[i - d - 1];
      day_version[i] = day_version[i - d - 1];
    } else {
      scanf("%s", s);
      string name = s;
      if (subjectToId.count(name) == 0) {
        subjectToId[name] = subjectToId.size() + 1;
        day_version[i] =
            updatePr(day_version[i], subjectToId[name], 1999999999);
      }
      int id = subjectToId[name];
      if (ss == "set") {
        int pr;
        scanf("%d", &pr);
        int oldPr = getPr(day_version[i], id);
        if (oldPr != 1999999999) {
          version[i] = updateSum(version[i], oldPr, -1);
        }
        day_version[i] = updatePr(day_version[i], id, pr);
        version[i] = updateSum(version[i], pr, +1);
      } else if (ss == "query") {
        int pr = getPr(day_version[i], id);
        if (pr == 1999999999) {
          printf("-1\n");
        } else {
          printf("%d\n", querySum(version[i], pr - 1));
        }
        fflush(stdout);
      } else if (ss == "remove") {
        int pr = getPr(day_version[i], id);
        version[i] = updateSum(version[i], pr, -1);
        day_version[i] = updatePr(day_version[i], id, 1999999999);
      }
    }
  }
}
