#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct treap {
  struct node {
    T key;
    int prio, sz;
    node *ch[2];
    node(T Key, int Prio = rand(), int Sz = 1, node *u = NULL, node *v = NULL) {
      key = Key, prio = Prio, sz = Sz, ch[0] = u, ch[1] = v;
    }
  };
  node *clone(node *u) {
    return new node(u->key, u->prio, u->sz, u->ch[0], u->ch[1]);
  }
  int size(node *u) { return !u ? 0 : u->sz; }
  node *update(node *u) {
    u->sz = size(u->ch[0]) + size(u->ch[1]) + 1;
    return u;
  }
  node *rotate(node *u, bool d) {
    if (u->ch[d]->prio <= u->prio) return update(u);
    node *v = clone(u->ch[d]);
    u->ch[d] = v->ch[!d];
    v->ch[!d] = update(u);
    return update(v);
  }
  node *insert(node *u, T key) {
    if (!u) return new node(key);
    node *c = clone(u);
    bool d = key > u->key;
    c->ch[d] = insert(c->ch[d], key);
    return rotate(c, d);
  }
  node *erase(node *u, T key) {
    if (!u) return u;
    node *c = clone(u);
    if (u->key == key) {
      if (!u->ch[0] || !u->ch[1]) return u->ch[!u->ch[0]];
      bool d = rand() & 1;
      node *v = u->ch[d];
      while (v->ch[!d] != NULL) v = v->ch[!d];
      c->key = v->key;
      c->ch[d] = erase(c->ch[d], c->key);
      return update(c);
    }
    bool d = c->key < key;
    c->ch[d] = erase(c->ch[d], key);
    return update(c);
  }
  pair<node *, node *> split(node *u, int k) {
    if (!u) return {u, u};
    node *c = clone(u);
    if (size(c->ch[0]) >= k) {
      auto s = split(c->ch[0], k);
      c->ch[0] = s.second;
      return {s.first, update(c)};
    }
    auto s = split(c->ch[1], k - size(c->ch[0]) - 1);
    c->ch[1] = s.first;
    return {update(c), s.second};
  }
  node *merge(node *u, node *v) {
    if (!u || !v) return u ? u : v;
    if (u->prio > v->prio) {
      node *c = clone(u);
      c->ch[1] = merge(c->ch[1], v);
      return update(c);
    }
    node *c = clone(v);
    c->ch[0] = merge(u, c->ch[0]);
    return update(c);
  }
  int less(node *u, T key) {
    if (!u) return 0;
    if (u->key < key) return size(u->ch[0]) + 1 + less(u->ch[1], key);
    return less(u->ch[0], key);
  }
  node *kth(node *u, int k) {
    if (!u || size(u->ch[0]) + 1 == k) return u;
    if (size(u->ch[0]) >= k) return kth(u->ch[0], k);
    return kth(u->ch[1], k - size(u->ch[0]) - 1);
  }
  node *insert2(node *u, T key) {
    auto s = split(u, less(u, key));
    return merge(merge(s.first, new node(key)), s.second);
  }
  node *erase2(node *u, T key) {
    auto s = split(u, less(u, key)), s2 = split(s.second, 1);
    if (s2.first && s2.first->key == key) return merge(s.first, s2.second);
    return u;
  }
  void dfs(node *u) {
    if (!u) return;
    cout << "l: ";
    dfs(u->ch[0]);
    cout << u->key << " ";
    cout << "r: ";
    dfs(u->ch[1]);
  }
  int depth(node *u) {
    if (!u) return 0;
    return 1 + max(dfs2(u->ch[0]), dfs2(u->ch[1]));
  }
};
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  srand((unsigned)time(NULL));
  treap<pair<string, int>> t;
  treap<int> v;
  vector<pair<treap<pair<string, int>>::node *, treap<int>::node *>> p(1);
  p[0].first = NULL;
  p[0].second = NULL;
  int q, x;
  cin >> q;
  string s, a;
  while (q--) {
    cin >> s;
    if (s[0] == 's') {
      cin >> a >> x;
      int k = t.less(p.back().first, make_pair(a, 1 << 30));
      auto r = t.kth(p.back().first, k);
      if (r && r->key.first == a) {
        auto s2 = v.erase(p.back().second, r->key.second);
        s2 = v.insert(s2, x);
        auto s1 = t.erase(p.back().first, r->key);
        s1 = t.insert(s1, {a, x});
        p.push_back({s1, s2});
      } else {
        auto s2 = v.insert(p.back().second, x);
        auto s1 = t.insert(p.back().first, {a, x});
        p.push_back({s1, s2});
      }
    } else if (s[0] == 'r') {
      cin >> a;
      int k = t.less(p.back().first, make_pair(a, 1 << 30));
      auto r = t.kth(p.back().first, k);
      if (r && r->key.first == a) {
        auto s2 = v.erase(p.back().second, r->key.second);
        auto s1 = t.erase(p.back().first, r->key);
        p.push_back({s1, s2});
      } else
        p.push_back(p.back());
    } else if (s[0] == 'q') {
      cin >> a;
      int k = t.less(p.back().first, make_pair(a, 1 << 30));
      auto r = t.kth(p.back().first, k);
      if (r && r->key.first == a)
        cout << v.less(p.back().second, r->key.second);
      else
        cout << "-1";
      cout << "\n" << flush;
      p.push_back(p.back());
    } else if (s[0] == 'u') {
      cin >> x;
      p.push_back(p[p.size() - 1 - x]);
    }
  }
  return 0;
}
