#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 15;
struct tree {
  int l, r, sum;
  tree *left, *right;
  tree(int L, int R, int val) {
    l = L, r = R, sum = val;
    left = right = NULL;
  }
};
tree *pq[N], *db[N];
tree *update(tree *cur, int l, int r, int ind, int add) {
  tree *ret = new tree(l, r, 0);
  ret->sum += cur->sum + add;
  if (l == r) {
    ret->sum = cur->sum;
    ret->sum += add;
    return ret;
  }
  int mid = (l + r) / 2;
  if (ind <= mid) {
    if (!cur->left) {
      cur->left = new tree(l, mid, 0);
    }
    ret->right = cur->right;
    ret->left = update(cur->left, l, mid, ind, add);
  } else {
    if (!cur->right) {
      cur->right = new tree(mid + 1, r, 0);
    }
    ret->right = update(cur->right, mid + 1, r, ind, add);
    ret->left = cur->left;
  }
  return ret;
}
int getval(tree *cur, int l, int r, int ind) {
  if (l == r) {
    return cur->sum;
  }
  int mid = (l + r) / 2;
  if (ind <= mid) {
    if (!cur->left) return 0;
    return getval(cur->left, l, mid, ind);
  } else {
    if (!cur->right) return 0;
    return getval(cur->right, mid + 1, r, ind);
  }
}
int count(tree *cur, int l, int r, int ind) {
  if (cur == NULL) return 0;
  if (ind < l) {
    return 0;
  }
  if (r <= ind) {
    return cur->sum;
  }
  int mid = (l + r) / 2;
  int ret = 0;
  if (cur->left) ret = count(cur->left, l, mid, ind);
  if (cur->right) ret += count(cur->right, mid + 1, r, ind);
  return ret;
}
map<string, int> Hash;
int main() {
  int q, i;
  string s, name;
  int pos = 1;
  int x;
  cin >> q;
  int n = 1e5;
  int m = 1e9;
  int C = 0, v;
  pq[C] = new tree(1, n, -1);
  db[C] = new tree(1, m, 0);
  while (q--) {
    cin >> s;
    if (s[0] == 'u') {
      scanf("%d", &x);
      pq[C + 1] = pq[C - x];
      db[C + 1] = db[C - x];
    } else {
      cin >> name;
      if (!Hash.count(name)) {
        Hash[name] = pos++;
      }
      int id = Hash[name];
      if (s[0] == 's') {
        scanf("%d", &x);
        v = getval(pq[C], 1, n, id);
        if (v != 0) {
          pq[C + 1] = update(pq[C], 1, n, id, -v + x);
          db[C + 1] = update(db[C], 1, m, v, -1);
          db[C + 1] = update(db[C + 1], 1, m, x, 1);
        } else {
          pq[C + 1] = pq[C], db[C + 1] = db[C];
          pq[C + 1] = update(pq[C + 1], 1, n, id, x);
          db[C + 1] = update(db[C + 1], 1, m, x, 1);
        }
      } else if (s[0] == 'q') {
        pq[C + 1] = pq[C];
        db[C + 1] = db[C];
        v = getval(pq[C + 1], 1, n, id);
        if (v != 0) {
          printf("%d\n", count(db[C + 1], 1, m, v - 1));
        } else
          printf("-1\n");
      } else {
        pq[C + 1] = pq[C];
        db[C + 1] = db[C];
        v = getval(pq[C + 1], 1, n, id);
        if (v != 0) {
          pq[C + 1] = update(pq[C + 1], 1, n, id, -v);
          db[C + 1] = update(db[C + 1], 1, m, v, -1);
        }
      }
    }
    ++C;
  }
}
