#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<long long> v(2 * 1e5 + 5, 0);
  vector<bool> check(2 * 1e5 + 5, 0);
  long long q;
  cin >> q;
  char c;
  long long l = 0, r = 0, n;
  while (q--) {
    cin >> c >> n;
    if (c == 'L') {
      l++;
      v[n] = l;
    } else if (c == 'R') {
      r++;
      v[n] = r;
      check[n] = 1;
    } else {
      long long total = l + r;
      if (check[n]) {
        long long rpos = r - v[n];
        cout << min(rpos, total - rpos - 1) << endl;
      } else {
        long long lpos = l - v[n];
        cout << min(lpos, total - lpos - 1) << endl;
      }
    }
  }
  return 0;
}
