#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 16;
struct Book {
  int l, r;
  int po;
};
Book a[N];
int main() {
  int l, r;
  char ch;
  int id;
  int q;
  scanf("%d", &q);
  l = r = 0;
  while (q--) {
    getchar();
    scanf("%c %d", &ch, &id);
    if (ch == 'L')
      l++, a[id].l = l, a[id].r = 0, a[id].po = 0;
    else if (ch == 'R')
      r++, a[id].r = r, a[id].l = 0, a[id].po = 1;
    else {
      if (a[id].po)
        printf("%d\n", min(l - 1 + a[id].r, r - a[id].r));
      else
        printf("%d\n", min(l - a[id].l, r - 1 + a[id].l));
    }
  }
  return 0;
}
