#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 300005;
struct segtree {
  int tree[30 * maxm], L[30 * maxm], R[30 * maxm];
  int sz = 0;
  int root[maxm];
  int update(int pre, int l, int r, int pos, int val) {
    if (l > r || pos < l || pos > r) return pre;
    int no = ++sz;
    tree[no] = tree[pre];
    L[no] = L[pre];
    R[no] = R[pre];
    if (l == r && l == pos) {
      tree[no] += val;
      R[no] = L[no] = 0;
      return no;
    }
    int mid = (l + r) >> 1;
    L[no] = update(L[pre], l, mid, pos, val);
    R[no] = update(R[pre], mid + 1, r, pos, val);
    tree[no] = tree[L[no]] + tree[R[no]];
    return no;
  }
  int sum(int no, int l, int r, int pos) {
    if (l == r) return 0;
    int mid = (l + r) >> 1;
    if (pos <= mid) return sum(L[no], l, mid, pos);
    return tree[L[no]] + sum(R[no], mid + 1, r, pos);
  }
  int get(int no, int l, int r, int pos) {
    if (l == r) return tree[no];
    int mid = (l + r) >> 1;
    if (pos <= mid) return get(L[no], l, mid, pos);
    return get(R[no], mid + 1, r, pos);
  }
} a, b;
map<string, int> mapa;
const int inf = 1e9 + 10;
int main() {
  int q;
  scanf("%d", &q);
  int idx = 1;
  for (int i = 1; i <= q; i++) {
    char op[15];
    scanf("%s", op);
    a.root[i] = a.root[i - 1];
    b.root[i] = b.root[i - 1];
    if (op[0] == 's') {
      char s[20];
      int val;
      scanf("%s %d", s, &val);
      string t = s;
      if (mapa.count(t)) {
        int x = a.get(a.root[i], 1, maxn, mapa[t]);
        a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], val - x);
        if (x != 0) {
          b.root[i] = b.update(b.root[i], 1, inf, x, -1);
        }
        b.root[i] = b.update(b.root[i], 1, inf, val, 1);
      } else {
        mapa[t] = idx++;
        a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], val);
        b.root[i] = b.update(b.root[i], 1, inf, val, 1);
      }
    } else if (op[0] == 'r') {
      char s[20];
      scanf("%s", s);
      string t = s;
      if (mapa.count(t)) {
        int x = a.get(a.root[i], 1, maxn, mapa[t]);
        a.root[i] = a.update(a.root[i], 1, maxn, mapa[t], -x);
        if (x != 0) {
          b.root[i] = b.update(b.root[i], 1, inf, x, -1);
        }
      }
    } else if (op[0] == 'q') {
      char s[20];
      scanf("%s", s);
      string t = s;
      if (mapa.count(t)) {
        int x = a.get(a.root[i], 1, maxn, mapa[t]);
        if (x != 0) {
          printf("%d\n", b.sum(b.root[i], 1, inf, x));
        } else
          puts("-1");
      } else
        puts("-1");
      fflush(stdout);
    } else {
      int x;
      scanf("%d", &x);
      a.root[i] = a.root[i - x - 1];
      b.root[i] = b.root[i - x - 1];
    }
  }
  return 0;
}
