#include <bits/stdc++.h>
const int maxn = 200005;
using namespace std;
int l[maxn + 5], r[maxn + 5];
int main() {
  int id, lnum = 0, rnum = 0, num = 0, q;
  char c;
  scanf("%d", &q);
  memset(l, -1, sizeof(l));
  memset(r, -1, sizeof(r));
  for (int i = 0; i < q; i++) {
    getchar();
    num++;
    scanf("%c %d", &c, &id);
    if (c == 'L')
      l[id] = ++lnum;
    else if (c == 'R')
      r[id] = ++rnum;
    else {
      num--;
      if (l[id] == -1) {
        int use = rnum - r[id];
        printf("%d\n", min(use, num - use - 1));
      } else {
        int use = lnum - l[id];
        printf("%d\n", min(use, num - use - 1));
      }
    }
  }
  return 0;
}
