#include <bits/stdc++.h>
int NEXT_FREE_ID = 0;
std::unordered_map<std::string, int> stringId;
namespace persistentid {
int NEXT_FREE_INDEX = 1;
int s[10000000], L[10000000], R[10000000];
int root[100005];
int n = 1 << 30;
int upd(int p, int v, int id, int l = 0, int r = n) {
  int ID = NEXT_FREE_INDEX++;
  s[ID] = s[id] + v;
  if (r - l < 2) return ID;
  int mid = (l + r) / 2;
  L[ID] = L[id], R[ID] = R[id];
  if (p < mid)
    L[ID] = upd(p, v, L[ID], l, mid);
  else
    R[ID] = upd(p, v, R[ID], mid, r);
  return ID;
}
int f(int p, int id, int l = 0, int r = n) {
  if (r - l < 2) return s[id];
  int mid = (l + r) / 2;
  if (p < mid)
    return f(p, L[id], l, mid);
  else
    return f(p, R[id], mid, r);
}
}  // namespace persistentid
namespace persistentcount {
int NEXT_FREE_INDEX = 1;
int s[10000000], L[10000000], R[10000000];
int root[100005];
int n = 1 << 30;
int upd(int p, int v, int id, int l = 0, int r = n) {
  int ID = NEXT_FREE_INDEX++;
  s[ID] = s[id] + v;
  if (r - l < 2) return ID;
  int mid = (l + r) / 2;
  L[ID] = L[id], R[ID] = R[id];
  if (p < mid)
    L[ID] = upd(p, v, L[ID], l, mid);
  else
    R[ID] = upd(p, v, R[ID], mid, r);
  return ID;
}
int sum(int x, int y, int id, int l = 0, int r = n) {
  if (x >= r || l >= y) return 0;
  if (x <= l && r <= y) return s[id];
  int mid = (l + r) / 2;
  return sum(x, y, L[id], l, mid) + sum(x, y, R[id], mid, r);
}
}  // namespace persistentcount
int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  std::cout.tie(0);
  int Q;
  std::cin >> Q;
  for (int q = 1; q <= Q; q++) {
    std::string type;
    std::cin >> type;
    if (type == "set") {
      std::string a;
      std::cin >> a;
      int x;
      std::cin >> x;
      if (stringId.count(a) == 0) {
        stringId[a] = NEXT_FREE_ID++;
        persistentid::root[q] = persistentid::NEXT_FREE_INDEX;
        persistentid::upd(stringId[a], x, persistentid::root[q - 1]);
        persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;
        persistentcount::upd(x, 1, persistentcount::root[q - 1]);
      } else {
        persistentid::root[q] = persistentid::NEXT_FREE_INDEX;
        int val = persistentid::f(stringId[a], persistentid::root[q - 1]);
        persistentid::upd(stringId[a], -val + x, persistentid::root[q - 1]);
        int temp = persistentcount::NEXT_FREE_INDEX;
        persistentcount::upd(val, -1, persistentcount::root[q - 1]);
        persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;
        persistentcount::upd(x, 1, temp);
      }
    } else if (type == "remove") {
      std::string a;
      std::cin >> a;
      if (stringId.count(a) != 0 &&
          persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) {
        int val = persistentid::f(stringId[a], persistentid::root[q - 1]);
        persistentcount::root[q] = persistentcount::NEXT_FREE_INDEX;
        persistentcount::upd(val, -1, persistentcount::root[q - 1]);
        persistentid::root[q] = persistentid::NEXT_FREE_INDEX;
        persistentid::upd(stringId[a], -val, persistentid::root[q - 1]);
      } else {
        persistentcount::root[q] = persistentcount::root[q - 1];
        persistentid::root[q] = persistentid::root[q - 1];
      }
    } else if (type == "query") {
      std::string a;
      std::cin >> a;
      persistentcount::root[q] = persistentcount::root[q - 1];
      persistentid::root[q] = persistentid::root[q - 1];
      if (stringId.count(a) != 0 &&
          persistentid::f(stringId[a], persistentid::root[q - 1]) != 0) {
        std::cout << persistentcount::sum(
                         1,
                         persistentid::f(stringId[a],
                                         persistentid::root[q - 1]),
                         persistentcount::root[q - 1])
                  << "\n"
                  << std::flush;
      } else {
        std::cout << "-1\n" << std::flush;
      }
    } else {
      int d;
      std::cin >> d;
      d = q - 1 - d;
      persistentcount::root[q] = persistentcount::root[d];
      persistentid::root[q] = persistentid::root[d];
    }
  }
}
