#include <bits/stdc++.h>
using namespace std;
void test_cases() {
  int n;
  cin >> n;
  map<int, int> l, r;
  int left = 1, right = 1;
  for (long long i = 0; i < n; i++) {
    char c;
    int second;
    cin >> c >> second;
    if (c == 'L') {
      l.insert({second, left});
      left++;
    } else if (c == 'R') {
      r.insert({second, right});
      right++;
    } else {
      if (l.find(second) != l.end()) {
        cout << min(left - 1 - l[second],
                    ((left + right - 2) - (left - l[second])))
             << '\n';
      } else if (r.find(second) != r.end()) {
        cout << min((left + right - 2) - (right - r[second]),
                    right - 1 - r[second])
             << '\n';
      }
    }
  }
}
int main(int argc, char const *argv[]) {
  int n = 1;
  for (long long i = 1; i <= n; ++i) {
    test_cases();
    cout << '\n';
  }
  return 0;
}
