#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int t, n, m;
int pos[MAXN];
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> t;
  int l = 2500000, r = 2499999;
  while (t--) {
    char op;
    cin >> op >> n;
    if (op == 'L') {
      l--;
      pos[n] = l;
    } else if (op == 'R') {
      r++;
      pos[n] = r;
    } else {
      cout << min(r - pos[n], pos[n] - l) << '\n';
    }
  }
}
