#include <bits/stdc++.h>
using namespace std;
struct StringTrie {
  int prefixes = 0;
  int words = 0;
  StringTrie* ch[26] = {
      nullptr,
  };
  StringTrie() {}
  StringTrie(StringTrie* old) {
    prefixes = old->prefixes;
    words = old->words;
    for (int i = 0; i < 26; ++i) {
      ch[i] = old->ch[i];
    }
  }
  ~StringTrie() {
    for (int i = 25; i >= 0; --i) {
      if (ch[i]) {
        delete ch[i];
      }
    }
  }
  StringTrie* set(string& s, int val, int p = 0) {
    StringTrie* rt = new StringTrie(this);
    rt->prefixes += (val > 0) ? 1 : -1;
    if (p >= int(s.size())) {
      rt->words = val;
    } else {
      int abc = int(s[p] - 'a');
      if (!ch[abc]) ch[abc] = new StringTrie();
      rt->ch[abc] = ch[abc]->set(s, val, p + 1);
    }
    return rt;
  }
  int get(string& s, int p = 0) {
    if (p >= int(s.size())) return words;
    int abc = int(s[p] - 'a');
    if (!ch[abc]) return 0;
    return ch[abc]->get(s, p + 1);
  }
};
struct BinaryTrie {
  int prefixes = 0;
  int words = 0;
  BinaryTrie* ch[2] = {
      nullptr,
  };
  BinaryTrie() {}
  BinaryTrie(BinaryTrie* old) {
    prefixes = old->prefixes;
    words = old->words;
    for (int i = 0; i < 2; ++i) {
      ch[i] = old->ch[i];
    }
  }
  ~BinaryTrie() {
    if (ch[0]) delete ch[0];
    if (ch[1]) delete ch[1];
  }
  BinaryTrie* add(int bs, int val, int p = 30) {
    BinaryTrie* rt = new BinaryTrie(this);
    rt->prefixes += (val > 0) ? 1 : -1;
    if (p < 0) {
      rt->words += val;
    } else {
      int id = (bs >> p) & 1;
      if (!ch[id]) ch[id] = new BinaryTrie();
      rt->ch[id] = ch[id]->add(bs, val, p - 1);
    }
    return rt;
  }
  int getprefix(int bs, int p = 30) {
    int res = 0;
    if (p >= 0) {
      if ((bs >> p) & 1) {
        if (ch[0]) res += ch[0]->prefixes;
        if (ch[1]) res += ch[1]->getprefix(bs, p - 1);
      } else {
        if (ch[0])
          res += ch[0]->getprefix(bs, p - 1);
        else
          return 0;
      }
    }
    return res;
  }
};
int main(int argc, char* argv[]) {
  {
    int n;
    cin >> n;
    vector<StringTrie*> sta(n + 1);
    vector<BinaryTrie*> bta(n + 1);
    string op;
    string assignment;
    int priority;
    sta[0] = new StringTrie();
    bta[0] = new BinaryTrie();
    for (int k = 1; k <= n; ++k) {
      cin >> op;
      if (op == "set") {
        cin >> assignment >> priority;
        sta[k] = sta[k - 1];
        bta[k] = bta[k - 1];
        int x = sta[k - 1]->get(assignment);
        sta[k] = sta[k - 1]->set(assignment, priority);
        if (x) {
          bta[k] = bta[k - 1]->add(x, -1);
          bta[k] = bta[k]->add(priority, 1);
        } else {
          bta[k] = bta[k - 1]->add(priority, 1);
        }
      } else if (op == "remove") {
        cin >> assignment;
        int x = sta[k - 1]->get(assignment);
        sta[k] = sta[k - 1]->set(assignment, 0);
        if (x) {
          bta[k] = bta[k - 1]->add(x, -1);
        } else {
          bta[k] = bta[k - 1];
        }
      } else if (op == "query") {
        cin >> assignment;
        int x = sta[k - 1]->get(assignment);
        cout << (x ? bta[k - 1]->getprefix(x) : -1) << '\n';
        sta[k] = sta[k - 1];
        bta[k] = bta[k - 1];
      } else {
        cin >> priority;
        sta[k] = sta[k - priority - 1];
        bta[k] = bta[k - priority - 1];
      }
    }
  }
}
