#include <bits/stdc++.h>
using namespace std;
int ABS(int x) {
  if (x < 0) return x * -1;
  return x;
}
int q;
int pos[2 * 100001];
int l, r;
int main() {
  cin >> q;
  bool first = true;
  while (q--) {
    char c;
    int x;
    cin >> c >> x;
    if (c == 'L') {
      if (first) {
        first = false;
        pos[x] = l;
      } else {
        l--;
        pos[x] = l;
      }
    } else if (c == 'R') {
      if (first) {
        first = false;
        pos[x] = r;
      } else {
        r++;
        pos[x] = r;
      }
    } else {
      cout << min(ABS(l - pos[x]), ABS(r - pos[x])) << endl;
    }
  }
  return 0;
}
