#include <bits/stdc++.h>
using namespace std;
const int N = 111111;
struct Tree {
  int cnt, rt[N], L[N * 60], R[N * 60], sum[N * 60];
  void add(int& x, int y, int l, int r, int p, int v) {
    x = ++cnt;
    L[x] = L[y], R[x] = R[y], sum[x] = sum[y] + v;
    if (l == r) return;
    int mid = (l + r) >> 1;
    (p <= mid) ? add(L[x], L[y], l, mid, p, v)
               : add(R[x], R[y], mid + 1, r, p, v);
  }
  int qry(int x, int l, int r, int ql, int qr) {
    if (!x) return 0;
    if (ql <= l && r <= qr) return sum[x];
    int mid = (l + r) >> 1, ret = 0;
    if (ql <= mid) ret += qry(L[x], l, mid, ql, qr);
    if (mid < qr) ret += qry(R[x], mid + 1, r, ql, qr);
    return ret;
  }
  void cg(int x, int y, int v) { add(rt[x], rt[x], 0, 1e9, y, v); }
  int qry(int x, int y) { return qry(rt[x], 0, 1e9, y, y); }
  int qry(int x, int l, int r) { return qry(rt[x], 0, 1e9, l, r); }
  void cp(int x, int y) { rt[x] = rt[y]; }
} A, B;
int n, idxcnt;
char op[10];
string s;
map<string, int> idx;
int main() {
  scanf("%d", &n);
  for (int i = 1, o, x; i <= n; ++i) {
    scanf("%s", op);
    A.cp(i, i - 1), B.cp(i, i - 1);
    if (op[0] == 's') {
      cin >> s;
      scanf("%d", &x);
      if (!idx[s])
        idx[s] = ++idxcnt, o = 0;
      else
        o = B.qry(i, idx[s]);
      A.cg(i, x, 1), B.cg(i, idx[s], x - o);
      if (o) A.cg(i, o, -1);
    }
    if (op[0] == 'r') {
      cin >> s;
      if (o = B.qry(i, idx[s])) B.cg(i, idx[s], -o), A.cg(i, o, -1);
    }
    if (op[0] == 'q') {
      cin >> s;
      if (o = B.qry(i, idx[s]))
        printf("%d\n", A.qry(i, 0, o - 1));
      else
        puts("-1");
    }
    if (op[0] == 'u') {
      scanf("%d", &x);
      A.cp(i, i - x - 1), B.cp(i, i - x - 1);
    }
    fflush(stdout);
  }
}
