#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n;
  cin >> n;
  long long right = 0, left = 0;
  map<long long, pair<long long, long long> > xxx;
  while (n--) {
    char xx;
    long long x;
    cin >> xx >> x;
    if (xx == 'L') {
      left++;
      xxx[x] = {-left + 1, left};
    } else if (xx == 'R') {
      right++;
      xxx[x] = {right, -right + 1};
    } else {
      cout << min(right - xxx[x].first, left - xxx[x].second) << "\n";
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  solve();
}
long long modPow(long long a, long long b) {
  if (b == 0) return 1;
  if (b % 2 == 0) {
    long long x = a * a;
    x %= 1000000007;
    return modPow(x, b / 2);
  }
  return (a * modPow(a, b - 1)) % 1000000007;
}
