#include <bits/stdc++.h>
using namespace std;
struct persistent_trie {
  struct node {
    vector<node*> child;
    int val;
    explicit node(int p) {
      child.resize(p, nullptr);
      val = 0;
    }
  };
  int alphabet;
  vector<node*> persist;
  persistent_trie(int states, int _alphabet) : alphabet(_alphabet) {
    persist.resize(states + 1, nullptr);
  }
  node* update_path(node* root, int val, vector<char>& v, int idx = 0) {
    if (root == nullptr) {
      root = new node(alphabet);
    }
    node* new_node = new node(alphabet);
    new_node->child = root->child;
    new_node->val = root->val + val;
    if (idx < (int)v.size())
      new_node->child[v[idx]] =
          update_path(root->child[v[idx]], val, v, idx + 1);
    return new_node;
  }
  node* set_leaf(node* root, int val, vector<char>& v, int idx = 0) {
    if (root == nullptr) {
      root = new node(alphabet);
    }
    node* new_node = new node(alphabet);
    new_node->child = root->child;
    new_node->val = root->val;
    if (idx < (int)v.size())
      new_node->child[v[idx]] = set_leaf(root->child[v[idx]], val, v, idx + 1);
    else {
      new_node->val = val;
    }
    return new_node;
  }
  int get_leaf_val(node* root, vector<char>& v) {
    for (int idx = 0; idx <= (int)v.size(); idx++) {
      if (root == nullptr) return 0;
      if (idx == (int)v.size()) return root->val;
      root = root->child[v[idx]];
    }
  }
  int smaller_vals(node* root, vector<char>& v) {
    int ans = 0;
    for (int idx = 0; idx < (int)v.size() and root != nullptr; idx++) {
      for (int i = 0; i < v[idx]; i++) {
        ans += root->child[i] == nullptr ? 0 : root->child[i]->val;
      }
      root = root->child[v[idx]];
    }
    return ans;
  }
  void copy_state(int from, int to) { persist[to] = persist[from]; }
};
void solve() {
  int q;
  cin >> q;
  persistent_trie m(q + 1, 10), a(q + 1, 2);
  map<string, int> d;
  for (int i = 1; i < q + 1; i++) {
    string ch;
    cin >> ch;
    if (ch == "undo") {
      int d;
      cin >> d;
      m.copy_state(i - d - 1, i);
      a.copy_state(i - d - 1, i);
    } else if (ch == "query") {
      string _s;
      cin >> _s;
      int s;
      if (d.count(_s))
        s = d[_s];
      else
        s = d[_s] = (int)d.size() + 1;
      vector<char> temp;
      while (s) {
        temp.push_back(s % 10);
        s /= 10;
      }
      int val = m.get_leaf_val(m.persist[i - 1], temp);
      if (val == 0) {
        cout << -1 << endl;
      } else {
        vector<char> temp2(30);
        for (int j = 0; j < 30; j++) {
          temp2[j] = (val >> (29 - j)) & 1;
        }
        cout << a.smaller_vals(a.persist[i - 1], temp2) << endl;
      }
      a.copy_state(i - 1, i);
      m.copy_state(i - 1, i);
    } else if (ch == "remove") {
      string _s;
      cin >> _s;
      int s;
      if (d.count(_s))
        s = d[_s];
      else
        s = d[_s] = (int)d.size() + 1;
      vector<char> temp;
      while (s) {
        temp.push_back(s % 10);
        s /= 10;
      }
      int val = m.get_leaf_val(m.persist[i - 1], temp);
      if (val == 0) {
        a.copy_state(i - 1, i);
        m.copy_state(i - 1, i);
      } else {
        vector<char> temp2(30);
        for (int j = 0; j < 30; j++) {
          temp2[j] = (val >> (29 - j)) & 1;
        }
        a.persist[i] = a.update_path(a.persist[i - 1], -1, temp2);
        m.persist[i] = m.set_leaf(m.persist[i - 1], 0, temp);
      }
    } else {
      string _s;
      cin >> _s;
      int s;
      if (d.count(_s))
        s = d[_s];
      else
        s = d[_s] = (int)d.size() + 1;
      vector<char> temp;
      while (s) {
        temp.push_back(s % 10);
        s /= 10;
      }
      int val = m.get_leaf_val(m.persist[i - 1], temp);
      if (val == 0) {
        cin >> val;
        vector<char> temp2(30);
        for (int j = 0; j < 30; j++) {
          temp2[j] = (val >> (29 - j)) & 1;
        }
        a.persist[i] = a.update_path(a.persist[i - 1], 1, temp2);
        m.persist[i] = m.set_leaf(m.persist[i - 1], val, temp);
      } else {
        vector<char> temp2(30);
        for (int j = 0; j < 30; j++) {
          temp2[j] = (val >> (29 - j)) & 1;
        }
        a.persist[i] = a.update_path(a.persist[i - 1], -1, temp2);
        m.persist[i] = m.set_leaf(m.persist[i - 1], 0, temp);
        cin >> val;
        for (int j = 0; j < 30; j++) {
          temp2[j] = (val >> (29 - j)) & 1;
        }
        a.persist[i] = a.update_path(a.persist[i], 1, temp2);
        m.persist[i] = m.set_leaf(m.persist[i], val, temp);
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
