#include <bits/stdc++.h>
using namespace std;
int main() {
  int le = 0, ld = 0, q, id;
  char op;
  scanf("%d", &q);
  map<int, int> mapa;
  scanf(" %c%d", &op, &id);
  mapa.insert(make_pair(id, 0));
  for (int i = 1; i < q; i++) {
    scanf(" %c%d", &op, &id);
    if (op == 'L') {
      le--;
      mapa.insert(make_pair(id, le));
    } else if (op == 'R') {
      ld++;
      mapa.insert(make_pair(id, ld));
    } else {
      printf("%d\n", min(mapa[id] - le, ld - mapa[id]));
    }
  }
  return 0;
}
