#include <bits/stdc++.h>
using namespace std;
int main() {
  int R = 0, L = -1;
  int n;
  cin >> n;
  vector<int> vec(200001);
  for (int i = 0; i < n; i++) {
    string c;
    cin >> c;
    int index;
    cin >> index;
    if (c == "L") {
      vec[index] = L;
      L--;
    } else if (c == "R") {
      vec[index] = R;
      R++;
    } else if (c == "?") {
      int ls = vec[index] + (abs(L) - 1);
      int rs = abs(vec[index] - (R - 1));
      cout << (ls > rs ? rs : ls) << endl;
    }
  }
}
