#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int l[N], r[N], rq[N];
pair<int, bool> pz[N];
int main() {
  char ch;
  int v, q;
  cin >> q;
  for (int i = 1; i <= q; ++i) {
    cin >> ch >> v;
    if (ch == 'L')
      l[i] = l[i - 1] + 1, pz[v] = {i, 0}, rq[i] = rq[i - 1] + 1,
      r[i] = r[i - 1];
    else if (ch == 'R')
      r[i] = r[i - 1] + 1, pz[v] = {i, 1}, rq[i] = rq[i - 1] + 1,
      l[i] = l[i - 1];
    else {
      int left(0), right(0);
      rq[i] = rq[i - 1];
      l[i] = l[i - 1];
      r[i] = r[i - 1];
      if (pz[v].second)
        left = rq[pz[v].first - 1];
      else
        right = rq[pz[v].first - 1];
      left += l[i] - l[pz[v].first];
      right += r[i] - r[pz[v].first];
      cout << min(left, right) << '\n';
    }
  }
  return 0;
}
