#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  while (cin >> q) {
    map<int, int> mp;
    for (int l = 0, r = 0, i = 0; i < q; i++) {
      string str;
      int id;
      cin >> str >> id;
      if (i == 0) {
        mp[id] = 0;
        l--;
        r++;
      } else if (str == "L") {
        mp[id] = l--;
      } else if (str == "R") {
        mp[id] = r++;
      } else {
        cout << min(mp[id] - l, r - mp[id]) - 1 << endl;
      }
    }
  }
  return 0;
}
