#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct node {
  node* L;
  node* R;
  int s, e, sum;
  node(node* L = 0, node* R = 0, int s = 0, int e = 0, int sum = 0)
      : L(L), R(R), s(s), e(e), sum(sum) {}
};
node* trees[2][N];
node* upd(node* root, int idx, int v) {
  node* ret = new node();
  ret->s = root->s;
  ret->e = root->e;
  ret->sum = root->sum + v;
  if (root->s == root->e) return ret;
  int md = (root->s + root->e) >> 1;
  if (idx <= md) {
    if (root->L == 0) root->L = new node(0, 0, root->s, md, 0);
    ret->L = upd(root->L, idx, v), ret->R = root->R;
  } else {
    if (root->R == 0) root->R = new node(0, 0, md + 1, root->e, 0);
    ret->R = upd(root->R, idx, v), ret->L = root->L;
  }
  return ret;
}
int get(node* root, int idx) {
  if (root->s == root->e) return root->sum;
  int md = (root->s + root->e) >> 1;
  if (idx <= md) {
    if (root->L)
      return get(root->L, idx);
    else
      return 0;
  } else {
    if (root->R)
      return get(root->R, idx);
    else
      return 0;
  }
}
int qry(node* root, int idx) {
  if (root->e < idx) return root->sum;
  if (root->s >= idx) return 0;
  int ret = 0;
  if (root->L) ret += qry(root->L, idx);
  if (root->R) ret += qry(root->R, idx);
  return ret;
}
map<string, int> mp;
char op[11];
char str[22];
int main() {
  trees[0][0] = new node(0, 0, 1, 1000000000, 0);
  trees[1][0] = new node(0, 0, 1, 100000, 0);
  int q;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%s", op);
    int idx;
    if (op[0] != 'u') {
      scanf("%s", str);
      if (!mp.count(string(str))) {
        int sz = mp.size();
        mp[string(str)] = sz;
      }
      idx = mp[string(str)];
    }
    ++idx;
    if (op[0] == 's') {
      int x;
      scanf("%d", &x);
      int v = get(trees[1][i], idx);
      trees[1][i + 1] = upd(trees[1][i], idx, -v + x);
      if (v) {
        node* tmp = upd(trees[0][i], v, -1);
        trees[0][i + 1] = upd(tmp, x, 1);
      } else
        trees[0][i + 1] = upd(trees[0][i], x, 1);
    } else if (op[0] == 'r') {
      int v = get(trees[1][i], idx);
      if (v) {
        trees[1][i + 1] = upd(trees[1][i], idx, -v);
        trees[0][i + 1] = upd(trees[0][i], v, -1);
      } else
        trees[0][i + 1] = trees[0][i], trees[1][i + 1] = trees[1][i];
    } else if (op[0] == 'q') {
      if (!get(trees[1][i], idx))
        puts("-1");
      else
        printf("%d\n", qry(trees[0][i], get(trees[1][i], idx)));
      fflush(stdout);
      trees[0][i + 1] = trees[0][i];
      trees[1][i + 1] = trees[1][i];
    } else {
      int x;
      scanf("%d", &x);
      trees[0][i + 1] = trees[0][i - x];
      trees[1][i + 1] = trees[1][i - x];
    }
  }
  return 0;
}
