#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1e9 + 7;
void solve() {
  int n;
  cin >> n;
  map<int, pair<char, int>> ma;
  int left = 0, right = 0;
  for (int i = 0; i < n; i++) {
    char ch;
    int x;
    cin >> ch >> x;
    if (ch == 'L') {
      left++;
      ma[x] = make_pair(ch, left);
    } else if (ch == 'R') {
      right++;
      ma[x] = make_pair(ch, right);
    } else {
      if (ma[x].first == 'L') {
        cout << min(left - ma[x].second, ma[x].second + right - 1) << "\n";
      } else {
        cout << min(right - ma[x].second, ma[x].second + left - 1) << "\n";
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL), cout.tie(NULL);
  solve();
  return 0;
}
