#include <bits/stdc++.h>
int main() {
  std::map<long, long> map;
  long q, id, leftMAX = 0, rightMAX = 0, firstTime = 0;
  char c;
  std::cin >> q;
  while (q--) {
    std::cin >> c >> id;
    if (!firstTime--) {
      map.insert(std::pair<long, long>(id, 0));
    } else {
      if (c == '?')
        std::cout << std::min(map[id] - leftMAX, rightMAX - map[id])
                  << std::endl;
      else if (c == 'R')
        map.insert(std::pair<long, long>(id, ++rightMAX));
      else
        map.insert(std::pair<long, long>(id, --leftMAX));
    }
  }
}
