#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long q, id;
  cin >> q;
  long long l = (q + 10) / 2;
  long long r = (q + 10) / 2 + 1;
  map<long long, long long> pos;
  char c;
  while (q-- > 0) {
    cin >> c >> id;
    if (c == 'L') {
      pos.emplace(id, l);
      l--;
    }
    if (c == 'R') {
      pos.emplace(id, r);
      r++;
    }
    if (c == '?') {
      cout << min(abs(l - pos[id]), abs(r - pos[id])) - 1 << endl;
    }
  }
}
