#include <bits/stdc++.h>
using namespace std;
long double sqr(long double a) { return a * a; }
long double gcd(long double a, long double b) {
  if (b == 0)
    return a;
  else
    return gcd(b, fmod(a, b));
}
pair<long long, long long> a[200005];
int main() {
  long long q, l = 0, r = 0;
  cin >> q;
  for (long long z = 0; z < q; ++z) {
    char tp;
    long long id;
    cin >> tp >> id;
    if (tp == 'L') {
      l++;
      a[id] = {l, 0};
    } else if (tp == 'R') {
      r++;
      a[id] = {0, r};
    } else {
      pair<long long, long long> tmp = a[id];
      long long lt, rt;
      if (tmp.first != 0) {
        lt = l - tmp.first;
        rt = r + tmp.first - 1;
      } else {
        lt = l + tmp.second - 1;
        rt = r - tmp.second;
      }
      cout << min(lt, rt) << "\n";
    }
  }
  return 0;
}
