#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N * 2];
int main() {
  int T;
  cin >> T;
  int l = N;
  int r = l - 1;
  while (T--) {
    string opt;
    int x;
    cin >> opt >> x;
    if (opt == "L") {
      a[x] = --l;
    } else if (opt == "R") {
      a[x] = ++r;
    } else if (opt == "?") {
      cout << min(a[x] - l, r - a[x]) << endl;
    }
  }
  return 0;
}
