#include <bits/stdc++.h>
using namespace std;
const int kNmax = 2e5 + 5;
int n;
char direction[kNmax];
int ids[kNmax];
int left_prefix[kNmax];
int right_prefix[kNmax];
int insertion_of[kNmax];
vector<pair<int, int> > queries;
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char dir;
    int x;
    cin >> dir >> x;
    if (dir == '?') {
      queries.emplace_back(i, x);
      continue;
    }
    direction[i] = dir;
    ids[i] = x;
    insertion_of[x] = i;
  }
  for (int i = 1; i <= n; i++) {
    left_prefix[i] = left_prefix[i - 1] + (direction[i] == 'L');
    right_prefix[i] = right_prefix[i - 1] + (direction[i] == 'R');
  }
  for (const auto& [query_index, id] : queries) {
    int insertion_index = insertion_of[id];
    int same_side_added =
        direction[insertion_index] == 'L'
            ? left_prefix[query_index] - left_prefix[insertion_index]
            : right_prefix[query_index] - right_prefix[insertion_index];
    int other_side_added = left_prefix[query_index] +
                           right_prefix[query_index] - same_side_added - 1;
    cout << min(same_side_added, other_side_added) << '\n';
  }
  return 0;
}
