#include <bits/stdc++.h>
using namespace std;
const int MAXN = 400005;
int a[MAXN];
int main() {
  int q;
  cin >> q;
  int l = 200005;
  int r = 200005;
  while (q--) {
    char s[3];
    int x;
    scanf("%s%d", s, &x);
    if (s[0] == 'L') {
      l--;
      a[x] = l;
    } else if (s[0] == 'R') {
      r++;
      a[x] = r;
    } else {
      int A = r - a[x];
      int B = a[x] - l;
      if (a[x] < 200005) {
        A = a[x] - l;
        B = r - 1 - a[x];
      } else {
        A = a[x] - 1 - l;
        B = r - a[x];
      }
      cout << min(A, B) << endl;
    }
  }
  return 0;
}
