#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int L[maxn], R[maxn];
int main() {
  int n;
  cin >> n;
  int Lcut = 0, Rcut = 0;
  for (int i = 0; i <= n; i++) L[i] = R[i] = 0;
  while (n--) {
    char str[2];
    int num;
    scanf("%s%d", str, &num);
    if (str[0] == 'L') {
      Lcut++;
      L[num] = Lcut;
    } else if (str[0] == 'R') {
      Rcut++;
      R[num] = Rcut;
    } else {
      if (L[num]) {
        int ans = min(Lcut - L[num], L[num] + Rcut - 1);
        printf("%d\n", ans);
      } else {
        int ans = min(Rcut - R[num], Lcut + R[num] - 1);
        printf("%d\n", ans);
      }
    }
  }
}
