#include <bits/stdc++.h>
using namespace std;
int main(int argc, char const *argv[]) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  long long int q, k;
  char ch;
  cin >> q;
  pair<long long int, long long int> mid;
  map<long long int, pair<char, long long int>> mp;
  cin >> ch >> k;
  mid = {0, 0};
  q--;
  while (q--) {
    cin >> ch >> k;
    if (ch == 'L') {
      mid.first++;
      mp[k] = {ch, mid.first};
    } else if (ch == 'R') {
      mid.second++;
      mp[k] = {ch, mid.second};
    } else {
      if (mp[k].first == 'L') {
        long long int mn = mid.first - mp[k].second;
        mn = min(mn, mid.second + (mp[k].second - 1) + 1);
        cout << mn << "\n";
      } else if (mp[k].first == 'R') {
        long long int mn = mid.second - mp[k].second;
        mn = min(mn, mid.first + (mp[k].second - 1) + 1);
        cout << mn << "\n";
      } else {
        cout << min(mid.first, mid.second) << "\n";
      }
    }
  }
  return 0;
}
