#include <bits/stdc++.h>
using namespace std;
int n, x, a[555555], j, q, p;
char c;
int main() {
  cin >> n;
  q = 1;
  p = 1;
  for (int i = 1; i <= n; i++) {
    cin >> c >> x;
    if (i == 1) {
      a[x] = q;
      q--;
      p++;
    } else {
      if (c == 'L') {
        a[x] = q;
        q--;
      } else if (c == 'R') {
        a[x] = p;
        p++;
      } else {
        cout << min(abs(a[x] - q), abs(a[x] - p)) - 1 << '\n';
      }
    }
  }
  return 0;
  getchar();
  getchar();
  return 0;
}
