#include <bits/stdc++.h>
using namespace std;
map<string, int> m;
struct node {
  int sum;
  node *l, *d;
  node() {
    sum = 0;
    l = d = NULL;
  }
};
node *copy(node *x) {
  node *ret = new node();
  if (x) {
    ret->sum = x->sum;
    ret->l = x->l;
    ret->d = x->d;
  }
  return ret;
}
int Sum(node *x) { return (x != NULL ? x->sum : 0); }
void update(node *x, int l, int d, int i, int val) {
  if (l > d || l > i || d < i) return;
  if (l == d && l == i) {
    x->sum += val;
    return;
  }
  int mid = (l + d) / 2;
  if (i <= mid) {
    x->l = copy(x->l);
    update(x->l, l, mid, i, val);
  } else {
    x->d = copy(x->d);
    update(x->d, mid + 1, d, i, val);
  }
  x->sum = Sum(x->l) + Sum(x->d);
}
int query(node *x, int l, int d, int i, int j) {
  if (x == NULL || l > d || l > j || d < i) return 0;
  if (i <= l && d <= j) return x->sum;
  int mid = (l + d) / 2;
  int ret = query(x->l, l, mid, i, j);
  ret += query(x->d, mid + 1, d, i, j);
  return ret;
}
node *ps[200111], *vl[200111];
int main() {
  int n, cnt = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    string t, str;
    int x;
    cin >> t;
    if (t == "undo") {
      cin >> x;
      vl[i] = copy(vl[i - x - 1]);
      ps[i] = copy(ps[i - x - 1]);
      continue;
    }
    cin >> str;
    vl[i] = copy(vl[i - 1]);
    ps[i] = copy(ps[i - 1]);
    if (!m.count(str)) m[str] = cnt++;
    int id = m[str];
    int pval = query(vl[i], 1, n, id, id);
    if (t == "set") {
      cin >> x;
      if (pval) update(ps[i], 0, (1000000000), pval, -1);
      update(ps[i], 0, (1000000000), x, 1);
      update(vl[i], 1, n, id, x - pval);
    }
    if (t == "remove") {
      if (pval) update(ps[i], 0, (1000000000), pval, -1);
      update(vl[i], 1, n, id, -pval);
    }
    if (t == "query") {
      if (!pval)
        cout << "-1" << endl;
      else
        cout << query(ps[i], 0, (1000000000), 0, pval - 1) << endl;
    }
  }
  return 0;
}
