#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  int l = 0, r = 1;
  map<int, int> mp;
  while (q--) {
    char c;
    cin >> c;
    long long int id;
    cin >> id;
    if (c == 'L')
      mp[id] = l--;
    else if (c == 'R')
      mp[id] = r++;
    else
      cout << min(mp[id] - l - 1, r - mp[id] - 1) << '\n';
  }
  return 0;
}
