#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
int test[maxn];
int main() {
  int n, num(0), x;
  cin >> n;
  char ch;
  int l = 2e5;
  int r = l + 1;
  int mi, ma;
  for (int i = 0; i < n; i++) {
    cin >> ch >> x;
    if (ch == 'L') {
      test[x] = l;
      num++;
      l--;
      mi = test[x];
    } else if (ch == 'R') {
      test[x] = r;
      num++;
      r++;
      ma = test[x];
    } else if (ch == '?') {
      int temp = test[x];
      int ans = min(temp - mi, r - 1 - temp);
      cout << ans << endl;
    }
  }
}
