#include <bits/stdc++.h>
using namespace std;
const int logN = 17;
const long long mod = 10000000000000061LL;
const int inf = 1e9;
const int N = 1e5 + 5;
int n, m, x, y, z;
char str[123], test[123];
class node {
 public:
  int sum;
  node *left, *right;
  node() {
    sum = 0;
    left = right = NULL;
  }
};
typedef node *pnode;
pnode ST[N], ST2[N], T[N], T2[N];
void init(pnode &t, long long bas, long long son) {
  if (!t) t = new node;
  return;
  if (bas == son) {
    t->sum = 0;
    return;
  }
  init(t->left, bas, ((bas + son) >> 1));
  init(t->right, ((bas + son) >> 1) + 1, son);
  t->sum = t->left->sum + t->right->sum;
}
int sum(pnode p) {
  if (!p) return 0;
  return p->sum;
}
void update(pnode &bef, pnode &cur, long long bas, long long son, long long x,
            long long y) {
  if (bas > x || son < x) {
    cur = bef;
    return;
  }
  if (!cur) {
    cur = new node;
    cur->sum = sum(bef);
  }
  if (bas == son) {
    cur->sum += y;
    return;
  }
  if (!bef) {
    bef = new node;
  }
  if (bas <= x && x <= ((bas + son) >> 1)) {
    cur->right = bef->right;
    update(bef->left, cur->left, bas, ((bas + son) >> 1), x, y);
  } else {
    cur->left = bef->left;
    update(bef->right, cur->right, ((bas + son) >> 1) + 1, son, x, y);
  }
  cur->sum = sum(cur->left) + sum(cur->right);
}
int query(pnode t, long long bas, long long son, long long x, long long y) {
  if (!t) return 0;
  if (bas > y || son < x) return 0;
  if (x <= bas && son <= y) return t->sum;
  return query(t->left, bas, ((bas + son) >> 1), x, y) +
         query(t->right, ((bas + son) >> 1) + 1, son, x, y);
}
long long get() {
  long long m = 0, nn = strlen(str + 1);
  for (int i = 1; i <= nn; i++) m = (m * 37LL + str[i] - 'a' + 1) % mod;
  return m + 1;
}
int main() {
  scanf("%d", &n);
  init(ST[0], 1ll, inf);
  init(ST2[0], 1ll, mod);
  for (int i = 1; i <= n; i++) {
    scanf("%s", test + 1);
    if (test[1] == 's') {
      scanf("%s", str + 1);
      long long t = get();
      scanf("%d", &x);
      long long k = query(ST2[i - 1], 1, mod, t, t);
      if (k) {
        update(ST[i - 1], T[i], 1, inf, k, -1);
        update(T[i], ST[i], 1, inf, x, +1);
        update(ST2[i - 1], T2[i], 1, mod, t, -k);
        update(T2[i], ST2[i], 1, mod, t, +x);
      } else {
        update(ST[i - 1], ST[i], 1, inf, x, 1);
        update(ST2[i - 1], ST2[i], 1, mod, t, x);
      }
    }
    if (test[1] == 'r') {
      scanf("%s", str + 1);
      long long t = get();
      long long k = query(ST2[i - 1], 1ll, mod, t, t);
      update(ST[i - 1], ST[i], 1ll, inf, k, -1);
      update(ST2[i - 1], ST2[i], 1ll, mod, t, -k);
    }
    if (test[1] == 'q') {
      scanf("%s", str + 1);
      long long t = get();
      ST[i] = ST[i - 1];
      ST2[i] = ST2[i - 1];
      int t1 = query(ST2[i], 1, mod, t, t);
      if (t1 == 0)
        printf("%d\n", -1);
      else
        printf("%d\n", query(ST[i], 1ll, (long long)inf, 1, t1 - 1));
      cout << flush;
    }
    if (test[1] == 'u') {
      scanf("%d", &x);
      ST[i] = ST[i - x - 1];
      ST2[i] = ST2[i - x - 1];
    }
  }
  return 0;
}
