#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int l = 0, s = 0, i, j, n, r = 0, c = 0, l2, t, k, m, a[200011];
  char ch;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> ch >> k;
    if (i == 1)
      a[k] = l, l--, r++;
    else {
      if (ch == 'L')
        a[k] = l, l--;
      else if (ch == 'R')
        a[k] = r, r++;
      else
        cout << min(abs(a[k] - l), abs(a[k] - r)) - 1 << "\n";
    }
  }
}
