#include <bits/stdc++.h>
using namespace std;
int n, id, b, e;
char c;
map<int, int> m;
int main() {
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> c >> id;
    if (c == 'L')
      m[id] = b--;
    else if (c == 'R')
      m[id] = ++e;
    else
      cout << min(m[id] - b - 1, e - m[id]) << endl;
  }
  return 0;
}
