#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long q;
  cin >> q;
  long long tot = 0;
  long long l = 0, r = 1;
  unordered_map<long long, long long> mp;
  while (q--) {
    char t;
    long long id;
    cin >> t >> id;
    if (t == 'L') {
      mp[id] = l--;
    } else if (t == 'R') {
      mp[id] = r++;
    } else {
      long long left = mp[id] - l - 1;
      long long right = r - mp[id] - 1;
      cout << min(left, right) << "\n";
    }
  }
}
