#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  int n;
  cin >> n;
  char ch;
  int idx;
  cin >> ch >> idx;
  int loc = 0, roc = 0;
  int dfc[200005];
  char where[200005];
  dfc[idx] = 0;
  for (int i = 1; i < n; i++) {
    cin >> ch >> idx;
    if (ch == 'L') {
      dfc[idx] = ++loc;
      where[idx] = ch;
    }
    if (ch == 'R') {
      dfc[idx] = ++roc;
      where[idx] = ch;
    }
    if (ch == '?') {
      if (where[idx] == 'L')
        cout << min(loc - dfc[idx], dfc[idx] + roc);
      else
        cout << min(roc - dfc[idx], dfc[idx] + loc);
      cout << '\n';
    }
  }
  return 0;
}
