#include <bits/stdc++.h>
using namespace std;
int main() {
  long long t;
  cin >> t;
  long long r = 0, l = 0, R = 0, L = 0;
  map<long long, long long> mpl;
  map<long long, long long> mpr;
  map<long long, long long> flag;
  int cnt = 0;
  char ch;
  long long k;
  cin >> ch >> k;
  mpl[k] = 0;
  mpr[k] = 0;
  if (ch == 'R')
    flag[k] = 0;
  else
    flag[k] = 1;
  t--;
  for (int i = 0; i < t; i++) {
    cin >> ch >> k;
    if (ch == 'R') {
      cnt++;
      flag[k] = 0;
      r++;
      mpr[k] = r;
    } else if (ch == 'L') {
      flag[k] = 1;
      l++;
      mpl[k] = l;
    } else {
      if (flag[k] == 0)
        cout << min(r - mpr[k], (l + mpr[k])) << endl;
      else
        cout << min(l - mpl[k], (r + mpl[k])) << endl;
    }
  }
}
