#include <bits/stdc++.h>
using namespace std;
int a[200100];
int main() {
  int q, id, l = 1, r = 1;
  char c;
  cin >> q;
  for (int zapros(0); zapros < q; zapros++) {
    cin >> c >> id;
    if (c == 'L') {
      l--;
      a[id] = l;
    }
    if (c == 'R') {
      a[id] = r;
      r++;
    }
    if (c == '?') cout << min(r - 1 - a[id], a[id] - l) << endl;
  }
  return 0;
}
