#include <bits/stdc++.h>
using namespace std;
int i, i0, n, m;
map<int, int> mp;
int main() {
  while (scanf("%d", &n) != EOF) {
    int l = 0, r = 0;
    while (n--) {
      char op;
      int tmp;
      scanf(" %c %d", &op, &tmp);
      if (op == 'L') {
        l--;
        mp[tmp] = l;
      }
      if (op == 'R') {
        mp[tmp] = r;
        r++;
      }
      if (op == '?') {
        printf("%d\n", min(r - mp[tmp] - 1, mp[tmp] - l));
      }
    }
  }
  return 0;
}
