#include <bits/stdc++.h>
using namespace std;
int q, id, lft, rght;
int poz[200005];
int absol(int a) {
  if (a >= 0)
    return a;
  else
    return -a;
}
int main() {
  lft = rght = 0;
  char x;
  cin >> q;
  cin.get();
  for (; q; q--) {
    cin >> x >> id;
    if (x == '?') {
      cout << min(rght - poz[id] - 1, absol(lft - poz[id] + 1)) << '\n';
    } else {
      if (lft == 0 && rght == 0) {
        lft--;
        rght++;
        poz[id] = 0;
      } else {
        if (x == 'L') {
          poz[id] = lft;
          lft--;
        } else {
          poz[id] = rght;
          rght++;
        }
      }
    }
  }
  return 0;
}
