#include <bits/stdc++.h>
using namespace std;
int pos[200003];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, x, l = 0, r = 0;
  char c;
  cin >> n;
  cin >> c >> x;
  n--;
  while (n--) {
    cin >> c >> x;
    if (c == 'L') {
      pos[x] = --l;
    } else if (c == 'R') {
      pos[x] = ++r;
    } else {
      cout << min(r - pos[x], pos[x] - l) << '\n';
    }
  }
  return 0;
}
