#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long l = 0, r = 0, mid, n, x, i, cn = 0;
  cin >> n;
  char ch;
  long long *arr = new long long[200005];
  for (i = 0; i < n; i++) {
    cin >> ch >> x;
    if (ch == 'R') {
      cn++;
      if (!i) {
        mid = x;
      } else {
        r++;
        arr[x] = r;
      }
    } else if (ch == 'L') {
      cn++;
      if (!i) {
        mid = x;
      } else {
        l--;
        arr[x] = l;
      }
    } else {
      arr[mid] = 0;
      long long a, b;
      if (arr[x] <= 0) {
        a = abs(l) - abs(arr[x]);
        b = cn - (a + 1);
        cout << min(a, b) << '\n';
      } else {
        a = abs(l) + arr[x];
        b = cn - (a + 1);
        cout << min(a, b) << '\n';
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
}
