#include <bits/stdc++.h>
long long kin() {
  long long x = 0;
  char c = getchar();
  bool neg = false;
  while (c != '-' && ('0' > c || c > '9')) c = getchar();
  if (c == '-') neg = true, c = getchar();
  while ('0' <= c && c <= '9') x = 10 * x + c - '0', c = getchar();
  if (neg) x = -x;
  return x;
}
void kout(int x) {
  if (x > 9) kout(x / 10);
  putchar(x % 10 + '0');
}
using namespace std;
const int N = 500010;
int q;
int t[4 * N], pos[N];
void up(int s, int l, int r, int i) {
  if (l > i || i > r) return;
  if (l == r) {
    t[s]++;
    return;
  }
  int m = (l + r) / 2;
  up(2 * s, l, m, i);
  up(2 * s + 1, m + 1, r, i);
  t[s] = t[2 * s] + t[2 * s + 1];
}
int get(int s, int l, int r, int u, int v) {
  if (l > v || u > r) return 0;
  if (u <= l && r <= v) return t[s];
  int m = (l + r) / 2;
  return get(2 * s, l, m, u, v) + get(2 * s + 1, m + 1, r, u, v);
}
int main() {
  int l = 200001, r = 200001;
  cin >> q;
  while (q--) {
    char c;
    int x;
    cin >> c >> x;
    if (c == 'L') {
      up(1, 1, 500000, --l);
      pos[x] = l;
    }
    if (c == 'R') {
      pos[x] = r;
      up(1, 1, 500000, r++);
    }
    if (c == '?') {
      cout << min(get(1, 1, 500000, 1, pos[x] - 1),
                  get(1, 1, 500000, pos[x] + 1, 500000))
           << "\n";
    }
  }
}
