#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  map<ll, ll> L, R;
  char c;
  ll in;
  map<ll, char> map;
  ll l = 0, r = 0;
  for (ll i = 0; i < (ll)n; i++) {
    cin >> c >> in;
    if (c == 'L') {
      L[in] = l;
      l++;
      map[in] = c;
    }
    if (c == 'R') {
      R[in] = r;
      r++;
      map[in] = c;
    }
    if (c == '?') {
      if (map[in] == 'R') {
        cout << min(r - R[in] - 1, R[in] + l) << endl;
      }
      if (map[in] == 'L') {
        cout << min(l - L[in] - 1, L[in] + r) << endl;
      }
    }
  }
}
