#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long arr[200005];
  long long l = 0, r = 1;
  long long t;
  cin >> t;
  while (t--) {
    char a;
    long long i;
    cin >> a >> i;
    if (a == 'L')
      arr[i] = l--;
    else if (a == 'R')
      arr[i] = r++;
    else
      cout << min(arr[i] - l - 1, r - arr[i] - 1) << endl;
  }
  return 0;
}
