#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 5;
struct Node {
  Node *ls, *rs;
  long long w;
} * cnt[MAXN], *pri[MAXN], *none;
int getid(string s) {
  static int tot = 0;
  static map<string, int> id;
  if (!id[s]) id[s] = ++tot;
  return id[s];
}
long long qry(Node *rt, int l, int r, int p, int q) {
  if (rt == none || q < p) return 0;
  if (l == p && r == q) return rt->w;
  if (q <= ((l + r) >> 1)) return qry(rt->ls, l, ((l + r) >> 1), p, q);
  if (p > ((l + r) >> 1)) return qry(rt->rs, ((l + r) >> 1) + 1, r, p, q);
  return qry(rt->ls, l, ((l + r) >> 1), p, ((l + r) >> 1)) +
         qry(rt->rs, ((l + r) >> 1) + 1, r, ((l + r) >> 1) + 1, q);
}
void upd(Node *&rt, int l, int r, int p, int delta) {
  Node *oldrt = rt;
  rt = new Node;
  *rt = *oldrt;
  rt->w += delta;
  if (l == r) return;
  if (p <= ((l + r) >> 1))
    upd(rt->ls, l, ((l + r) >> 1), p, delta);
  else
    upd(rt->rs, ((l + r) >> 1) + 1, r, p, delta);
}
int main() {
  none = new Node;
  none->ls = none->rs = none, none->w = 0;
  cnt[0] = pri[0] = none;
  int q, k;
  string op, s;
  cin >> q;
  for (int i = 1; i <= q; i++) {
    cin >> op;
    cnt[i] = cnt[i - 1], pri[i] = pri[i - 1];
    if (op[0] == 's') {
      cin >> s >> k;
      int id = getid(s), tmp = qry(pri[i], 1, q, id, id);
      if (tmp) upd(cnt[i], 1, 1e9, tmp, -1), upd(pri[i], 1, q, id, -tmp);
      upd(cnt[i], 1, 1e9, k, 1), upd(pri[i], 1, q, id, k);
    }
    if (op[0] == 'r') {
      cin >> s;
      int id = getid(s), tmp = qry(pri[i], 1, q, id, id);
      if (tmp) upd(cnt[i], 1, 1e9, tmp, -1), upd(pri[i], 1, q, id, -tmp);
    }
    if (op[0] == 'q') {
      cin >> s;
      int id = getid(s), tmp = qry(pri[i], 1, q, id, id);
      if (!tmp)
        cout << -1 << endl;
      else
        cout << qry(cnt[i], 1, 1e9, 1, tmp - 1) << endl;
      cout << flush;
    }
    if (op[0] == 'u') {
      cin >> k;
      cnt[i] = cnt[i - k - 1], pri[i] = pri[i - k - 1];
    }
  }
  return 0;
}
