#include <bits/stdc++.h>
using namespace std;
const int inf = 0x7FFFFFFF;
long long _GCD(long long a, long long b) { return b ? _GCD(b, a % b) : a; }
long long solution();
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  ;
  int tc = 1, cas = 0;
  while (tc--) {
    solution();
  }
  return 0;
}
long long solution() {
  int i, j, k, idx = 5 * 1000000;
  char ch;
  int L = idx - 1, R = idx, q;
  map<int, int> umap;
  cin >> q;
  while (q--) {
    cin >> ch >> k;
    if (ch == 'L') {
      umap[k] = L;
      L--;
    } else if (ch == 'R') {
      umap[k] = R;
      R++;
    } else if (ch == '?') {
      cout << min(R - umap[k], umap[k] - L) - 1 << endl;
    }
  }
  return 0;
}
