#include <bits/stdc++.h>
using namespace std;
const long long int INF = 1e15;
const long long int MOD = 1e9 + 7;
signed main() {
  long long int n;
  cin >> n;
  long long int l, r;
  l = r = 0;
  map<long long int, long long int> lm, rm;
  for (long long int i = 0; i < (n); ++i) {
    char c;
    long long int id;
    cin >> c >> id;
    if (c == 'L') {
      lm.emplace(id, ++l);
    }
    if (c == 'R') {
      rm.emplace(id, ++r);
    }
    if (c == '?') {
      long long int ll, rr;
      if (lm.find(id) != lm.end()) {
        ll = l - lm[id];
      } else {
        ll = rm[id] + l - 1;
      }
      if (rm.find(id) != rm.end()) {
        rr = r - rm[id];
      } else {
        rr = lm[id] + r - 1;
      }
      cout << min(ll, rr) << endl;
    }
  }
  return 0;
}
