#include <bits/stdc++.h>
using namespace std;
int main() {
  long q, suml = 0, sumr = 0;
  cin >> q;
  vector<pair<long, long>> v(200001, {0, 0});
  for (int i = 0; i < q; i++) {
    char a;
    long x;
    cin >> a >> x;
    if (a == 'L') {
      suml++;
      v[x].first = suml;
      v[x].second = 0;
    }
    if (a == 'R') {
      sumr++;
      v[x].first = sumr;
      v[x].second = 1;
    }
    if (a == '?') {
      long now1, now2;
      if (v[x].second == 0) {
        now1 = suml - v[x].first;
        now2 = sumr + v[x].first - 1;
        cout << min(now1, now2) << endl;
      }
      if (v[x].second == 1) {
        now1 = sumr - v[x].first;
        now2 = suml + v[x].first - 1;
        cout << min(now1, now2) << endl;
      }
    }
  }
}
