#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)1e18;
const long long mod = (long long)1e9 + 7;
const double eps = (double)1e-9;
const double pi = acos(-1.0);
const int dx[] = {0, 0, 1, 0, -1};
const int dy[] = {0, 1, 0, -1, 0};
const int N = 100500;
int q;
string name;
struct node {
  int sum;
  node *l, *r;
  node(){};
  node(int x) {
    sum = x;
    l = r = NULL;
  }
};
node *root[N];
struct bor {
  int id;
  bor *nxt[26];
  bor(){};
  bor(int x) {
    id = x;
    for (int i = 0; i < 26; i++) nxt[i] = NULL;
  }
};
bor *rtbor[N];
inline void clone(node *&nw, node *&pr) {
  if (!nw) nw = new node(0);
  if (!pr) pr = new node(0);
  nw->sum = pr->sum;
  nw->l = pr->l;
  nw->r = pr->r;
}
inline int getsum(node *&v) { return (v ? v->sum : 0); }
inline void upd(node *&v) {
  if (v) v->sum = getsum(v->l) + getsum(v->r);
}
node *upd(node *&v, int l, int r, int pos, int add) {
  if (!v) v = new node(0);
  node *u = new node(0);
  clone(u, v);
  if (l == r) {
    u->sum += add;
    return u;
  }
  int mid = (l + r) >> 1;
  if (pos <= mid)
    u->l = upd(v->l, l, mid, pos, add);
  else
    u->r = upd(v->r, mid + 1, r, pos, add);
  upd(u);
  return u;
}
int get(node *&v, int l, int r, int lx, int rx) {
  if (!v || rx < l || r < lx) return 0;
  if (lx <= l && r <= rx) return v->sum;
  int mid = (l + r) >> 1;
  return get(v->l, l, mid, lx, rx) + get(v->r, mid + 1, r, lx, rx);
}
inline int getid(bor *v) {
  if (!v) return -1;
  for (int i = 0; i < (int)name.size(); i++) {
    v = v->nxt[name[i] - 'a'];
    if (!v) return -1;
  }
  return v->id;
}
inline void clone1(bor *&nw, bor *&pr) {
  if (!nw) nw = new bor(-1);
  if (!pr) pr = new bor(-1);
  nw->id = pr->id;
  for (int i = 0; i < 26; i++) nw->nxt[i] = pr->nxt[i];
}
bor *upd(bor *&v, int val, int pos) {
  if (!v) v = new bor(-1);
  bor *u = new bor(-1);
  clone1(u, v);
  if (pos == (int)name.size()) {
    u->id = val;
    return u;
  }
  u->nxt[name[pos] - 'a'] = upd(v->nxt[name[pos] - 'a'], val, pos + 1);
  return u;
}
inline string getstr() {
  char c[20];
  scanf("%s", c);
  int len = (int)strlen(c);
  string res = "";
  for (int i = 0; i < len; i++) res += c[i];
  return res;
}
int main() {
  scanf("%d", &q);
  for (int i = 0; i <= q; i++) {
    root[i] = NULL;
    rtbor[i] = NULL;
  }
  for (int i = 1; i <= q; i++) {
    string tp = getstr();
    clone(root[i], root[i - 1]);
    clone1(rtbor[i], rtbor[i - 1]);
    if (tp == "set") {
      name = getstr();
      int was = getid(rtbor[i]);
      if (was != -1) root[i] = upd(root[i], 1, (int)1e9, was, -1);
      int nw;
      scanf("%d", &nw);
      root[i] = upd(root[i], 1, (int)1e9, nw, 1);
      rtbor[i] = upd(rtbor[i], nw, 0);
    } else if (tp == "remove") {
      name = getstr();
      int curid = getid(rtbor[i]);
      if (curid != -1) {
        root[i] = upd(root[i], 1, (int)1e9, curid, -1);
        rtbor[i] = upd(rtbor[i], -1, 0);
      }
    } else if (tp == "query") {
      name = getstr();
      int curid = getid(rtbor[i]);
      int ans = -1;
      if (curid != -1) {
        ans = 0;
        if (curid > 1) ans = get(root[i], 1, (int)1e9, 1, curid - 1);
      }
      printf("%d\n", ans);
      fflush(stdout);
    } else {
      int dif;
      scanf("%d", &dif);
      clone(root[i], root[i - dif - 1]);
      clone1(rtbor[i], rtbor[i - dif - 1]);
    }
  }
  return 0;
}
