#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 200100;
int l[maxn], r[maxn], a[maxn];
int lx, rx;
int main(void) {
  char cmd[4];
  int q, id;
  scanf("%d", &q);
  while (q--) {
    scanf("%s%d", cmd, &id);
    if (cmd[0] == 'L') {
      l[++lx] = id;
      a[id] = 1;
    } else if (cmd[0] == 'R')
      r[++rx] = id;
    else {
      if (a[id]) {
        int ans1 = 0, ans2 = 0;
        for (int i = lx; i >= 1; i--) {
          if (l[i] == id) break;
          ans1++;
        }
        ans2 = lx - ans1 + rx - 1;
        if (ans1 < ans2)
          printf("%d\n", ans1);
        else
          printf("%d\n", ans2);
        continue;
      } else {
        int ans1 = 0, ans2 = 0;
        for (int i = rx; i >= 1; i--) {
          if (r[i] == id) break;
          ans2++;
        }
        ans1 = rx - ans2 + lx - 1;
        if (ans1 < ans2)
          printf("%d\n", ans1);
        else
          printf("%d\n", ans2);
      }
    }
  }
  return 0;
}
