#include <bits/stdc++.h>
using namespace std;
int main() {
  int q;
  cin >> q;
  vector<int> arr(600005, 0);
  map<int, int> ids;
  int begin = 300000;
  int leftmost = begin;
  int rightmost = begin;
  for (int i = 0; i < q; i++) {
    char c;
    cin >> c;
    int t;
    cin >> t;
    if (c == 'L') {
      ids.insert({t, leftmost});
      leftmost = leftmost - 1;
      if (i == 0) rightmost = rightmost + 1;
    } else if (c == 'R') {
      ids.insert({t, rightmost});
      rightmost = rightmost + 1;
      if (i == 0) leftmost = leftmost - 1;
    } else {
      int pos = ids[t];
      int a = abs(leftmost - pos);
      int b = abs(rightmost - pos);
      cout << min(a, b) - 1 << endl;
    }
  }
}
