#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n;
map<string, int> id;
struct segtree {
  int sz, root[N + 1];
  struct node {
    int lson, rson, cnt;
  } nd[N << 6];
  void sprup(int p) { nd[p].cnt = nd[nd[p].lson].cnt + nd[nd[p].rson].cnt; }
  int nwnd() { return nd[++sz] = nd[0], sz; }
  void init() {
    sz = root[0] = 1;
    nd[0] = nd[1] = node({0, 0, 0});
  }
  void add(int x, int v, int p, int np, int tl = 0, int tr = 1e9) {
    if (tl == tr) return nd[np].cnt = nd[p].cnt + v, void();
    int mid = tl + tr >> 1;
    if (x <= mid) {
      nd[np].lson = nwnd(), nd[np].rson = nd[p].rson;
      add(x, v, nd[p].lson, nd[np].lson, tl, mid);
    } else {
      nd[np].rson = nwnd(), nd[np].lson = nd[p].lson;
      add(x, v, nd[p].rson, nd[np].rson, mid + 1, tr);
    }
    sprup(np);
  }
  int _cnt(int l, int r, int p, int tl = 0, int tr = 1e9) {
    if (l <= tl && r >= tr) return nd[p].cnt;
    int mid = tl + tr >> 1, res = 0;
    if (l <= mid) res += _cnt(l, r, nd[p].lson, tl, mid);
    if (r > mid) res += _cnt(l, r, nd[p].rson, mid + 1, tr);
    return res;
  }
} segt, pri;
int main() {
  cin >> n;
  segt.init(), pri.init();
  int tmp;
  for (int i = 1; i <= n; i++) {
    string tp, a;
    int x;
    cin >> tp;
    if (tp == "set") {
      cin >> a >> x;
      if (id.find(a) == id.end()) {
        int tmp = id.size();
        id[a] = tmp + 1;
      }
      int k = id[a];
      int fd = pri._cnt(k, k, pri.root[i - 1]);
      if (fd == 0)
        pri.add(k, x, pri.root[i - 1], pri.root[i] = pri.nwnd()),
            segt.add(x, 1, segt.root[i - 1], segt.root[i] = segt.nwnd());
      else
        pri.add(k, x - fd, pri.root[i - 1], pri.root[i] = pri.nwnd()),
            segt.add(fd, -1, segt.root[i - 1], tmp = segt.nwnd()),
            segt.add(x, 1, tmp, segt.root[i] = segt.nwnd());
    } else if (tp == "remove") {
      cin >> a;
      if (id.find(a) == id.end()) {
        pri.root[i] = pri.root[i - 1], segt.root[i] = segt.root[i - 1];
        continue;
      }
      int k = id[a];
      int fd = pri._cnt(k, k, pri.root[i - 1]);
      if (!fd)
        pri.root[i] = pri.root[i - 1], segt.root[i] = segt.root[i - 1];
      else
        pri.add(k, -fd, pri.root[i - 1], pri.root[i] = pri.nwnd()),
            segt.add(fd, -1, segt.root[i - 1], segt.root[i] = segt.nwnd());
    } else if (tp == "query") {
      pri.root[i] = pri.root[i - 1], segt.root[i] = segt.root[i - 1];
      cin >> a;
      if (id.find(a) == id.end()) {
        puts("-1");
        continue;
      }
      int k = id[a];
      int fd = pri._cnt(k, k, pri.root[i]);
      if (!fd)
        puts("-1");
      else
        printf("%d\n", segt._cnt(0, fd - 1, segt.root[i]));
    } else {
      cin >> x;
      pri.root[i] = pri.root[i - x - 1];
      segt.root[i] = segt.root[i - x - 1];
    }
  }
  return 0;
}
