#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  char op;
  int n, l = 1, r = 0;
  map<int, int> m;
  while (q-- > 0) {
    cin >> op >> n;
    if (op == 'L')
      m[n] = --l;
    else if (op == 'R')
      m[n] = ++r;
    else if (op == '?')
      cout << min(m[n] - l, r - m[n]) << endl;
  }
  return 0;
}
