#include <bits/stdc++.h>
using namespace std;
const int M = 2e5 + 5;
int q, n, l, r;
char x, t;
map<int, int> book;
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> q;
  l = r = M;
  for (int k = 1; k <= q; k++) {
    cin >> t >> n;
    if (k == 1) {
      if (t == 'L')
        book[n] = l--, r++;
      else
        book[n] = r++, l--;
    } else if (t == 'L')
      book[n] = l--;
    else if (t == 'R')
      book[n] = r++;
    else
      cout << min(book[n] - l - 1, r - book[n] - 1) << '\n';
  }
  return 0;
}
