#include <bits/stdc++.h>
using namespace std;
long n, l = 0, r = 0;
pair<int, char> a[200100];
int main() {
  cin >> n;
  char t;
  long id;
  for (int i = 1; i <= n; i++) {
    cin >> t >> id;
    if (t != '?') {
      if (t == 'L') {
        l++;
        a[id] = pair<int, char>(l, t);
      } else {
        r++;
        a[id] = pair<int, char>(r, t);
      }
    } else {
      if (a[id].second == 'L')
        cout << min(l - a[id].first, r + a[id].first - 1) << endl;
      else
        cout << min(l + a[id].first - 1, r - a[id].first) << endl;
    }
  }
}
