#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); }
int main() {
  int q;
  scanf("%d", &q);
  char c;
  int indx;
  int minleft = 2 * 100000, mxright = 2 * 100000;
  map<int, int> mp;
  int i = 0;
  while (q--) {
    cin >> c >> indx;
    if (i == 0) {
      mp[indx] = mxright;
      minleft--;
      mxright++;
      i++;
    } else {
      if (c == 'R') {
        mp[indx] = mxright;
        mxright++;
      } else if (c == 'L') {
        mp[indx] = minleft;
        minleft--;
      } else {
        printf("%d\n", min((mp[indx] - minleft) - 1, (mxright - mp[indx]) - 1));
      }
    }
  }
  return 0;
}
