#include <bits/stdc++.h>
using namespace std;
int oo = (1e9) + 7;
long long oo2 = 1ll * oo * oo;
int positions[200005];
int offsets[200005];
int main() {
  int q;
  cin >> q;
  list<int> shelf;
  int offset = 0;
  while (q--) {
    char type;
    int id;
    cin >> type >> id;
    if (type == 'L') {
      if (!shelf.empty()) offset++;
      shelf.push_front(id);
      positions[id] = 0;
      offsets[id] = (-1) * offset;
    } else if (type == 'R') {
      shelf.push_back(id);
      positions[id] = shelf.size() - 1;
      offsets[id] = (-1) * offset;
    } else if (type == '?') {
      int posid = positions[id] + offset + offsets[id];
      int topr2 = shelf.size() - posid - 1;
      printf("%d\n", min(posid, topr2));
    }
  }
  return 0;
}
