#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  int l = 0, r = 0;
  vector<int> ID(200009, INT_MAX);
  q--;
  char xx;
  int idd;
  cin >> xx >> idd;
  ID[idd] = 0;
  while (q > 0 && q--) {
    char x;
    int id;
    cin >> x >> id;
    if (x == 'L') {
      ID[id] = --l;
    } else if (x == 'R') {
      ID[id] = ++r;
    } else {
      cout << min(abs(ID[id] - l), abs(ID[id] - r)) << endl;
    }
  }
  return 0;
}
