#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 3e4;
long long lp[N], rp[N], l, r, first, second, m, so, k, n;
char ch;
int main() {
  cin >> n;
  for (long long i = (long long)1; i <= (long long)n; i++) {
    cin >> ch >> k;
    if (ch == '?') {
      first = l - lp[k];
      second = r - rp[k];
      m = l + r;
      so = 1e15;
      if (rp[k]) {
        so = min(so, r - rp[k]);
        so = min(so, m - second - 1);
      }
      if (lp[k]) {
        so = min(so, l - lp[k]);
        so = min(so, m - first - 1);
      }
      cout << so << "\n";
    } else {
      if (ch == 'L') lp[k] = ++l;
      if (ch == 'R') rp[k] = ++r;
    }
  }
}
