#include <bits/stdc++.h>
using namespace std;
map<int, char> dir;
int main() {
  map<int, int> left;
  map<int, int> right;
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int f;
  char x;
  int ll, rr;
  ll = rr = 0;
  while (n--) {
    cin >> x >> f;
    if (x == 'L') {
      ll++;
      left[f] = ll;
      dir[f] = x;
    } else if (x == 'R') {
      rr++;
      right[f] = rr;
      dir[f] = x;
    } else {
      if (dir[f] == 'L') {
        cout << min(ll - left[f], left[f] - 1 + rr) << endl;
      } else {
        cout << min(rr - right[f], right[f] - 1 + ll) << endl;
      }
    }
  }
}
