#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  map<int, int> l, r;
  int x = 0, y = 0;
  while (t--) {
    char a;
    int id;
    cin >> a >> id;
    if (a == 'L') {
      x++;
      l[id] = x;
    } else if (a == 'R') {
      y++;
      r[id] = y;
    } else {
      if (l.find(id) != l.end()) {
        cout << min(x - l[id], y + l[id] - 1) << endl;
      } else {
        cout << min(y - r[id], x + r[id] - 1) << endl;
      }
    }
  }
  return 0;
}
