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