#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int MX = 1e9 + 5;
struct Node {
  Node *left, *right;
  int val;
  bool created;
  Node() {
    val = 0;
    left = right = NULL;
    created = false;
  }
};
Node* root[N];
Node* root2[N];
Node* modify(int l, int r, int pos, Node* node, int val) {
  Node* ret = new Node();
  if (l == r) {
    assert(l == pos);
    ret->val = val;
    if (node != NULL) ret->val += node->val;
    return ret;
  }
  int mid = (l + r) / 2;
  if (pos <= mid) {
    ret->left = modify(l, mid, pos, node == NULL ? NULL : node->left, val);
    if (node != NULL) ret->right = node->right;
  } else {
    ret->right =
        modify(mid + 1, r, pos, node == NULL ? NULL : node->right, val);
    if (node != NULL) ret->left = node->left;
  }
  if (ret->left != NULL) ret->val += ret->left->val;
  if (ret->right != NULL) ret->val += ret->right->val;
  return ret;
}
int query(int l, int r, int x, int y, Node* node) {
  if (node == NULL) return 0;
  if (l >= x && r <= y) {
    return node->val;
  }
  if (y < l || x > r) return 0;
  int mid = (l + r) / 2;
  return query(l, mid, x, y, node->left) + query(mid + 1, r, x, y, node->right);
}
int s_id = 1;
map<string, int> ids;
inline void add_string(string s) {
  if (ids.count(s)) return;
  ids[s] = s_id++;
}
int main() {
  int q;
  cin >> q;
  root[0] = root2[0] = NULL;
  for (int root_id = 0; root_id < q; root_id++) {
    string opr;
    cin >> opr;
    if (opr[0] == 's') {
      string assign;
      int priority;
      cin >> assign;
      cin >> priority;
      add_string(assign);
      int old_val = query(1, MX, ids[assign], ids[assign], root2[root_id]);
      if (priority != old_val) {
        root2[root_id] =
            modify(1, MX, ids[assign], root2[root_id], priority - old_val);
        if (old_val) {
          Node* temp = modify(1, MX, old_val, root[root_id], -1);
          root[root_id] = modify(1, MX, priority, temp, 1);
        } else
          root[root_id] = modify(1, MX, priority, root[root_id], 1);
      }
    } else if (opr[0] == 'r') {
      string assign;
      cin >> assign;
      add_string(assign);
      int old_val = query(1, MX, ids[assign], ids[assign], root2[root_id]);
      if (old_val) {
        root[root_id] = modify(1, MX, old_val, root[root_id], -1);
        root2[root_id] = modify(1, MX, ids[assign], root2[root_id], -old_val);
        assert(query(1, MX, ids[assign], ids[assign], root2[root_id]) == 0);
      }
    } else if (opr[0] == 'q') {
      string assign;
      cin >> assign;
      add_string(assign);
      int old_val = query(1, MX, ids[assign], ids[assign], root2[root_id]);
      if (!old_val)
        cout << -1 << endl;
      else if (old_val == 1)
        cout << 0 << endl;
      else
        cout << query(1, MX, 1, old_val - 1, root[root_id]) << endl;
      cout << flush;
    } else {
      int di;
      cin >> di;
      root[root_id] = root[root_id - di - 1];
      root2[root_id] = root2[root_id - di - 1];
    }
    root[root_id + 1] = root[root_id];
    root2[root_id + 1] = root2[root_id];
  }
  return 0;
}
