#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  int l = 0, r = 0, n = 0, prev = -1;
  map<int, pair<int, int>> mp;
  map<int, pair<int, int>> mplr;
  while (t--) {
    char dir;
    int id;
    cin >> dir >> id;
    if (dir == 'L') {
      mp[id] = make_pair(0, n);
      l++;
      mplr[id] = make_pair(l, r);
      n++;
    } else if (dir == 'R') {
      mp[id] = make_pair(n, 0);
      r++;
      mplr[id] = make_pair(l, r);
      n++;
    } else {
      cout << min(mp[id].first + (l - mplr[id].first),
                  mp[id].second + (r - mplr[id].second))
           << endl;
    }
  }
  return 0;
}
