#include <bits/stdc++.h>
using namespace std;
int q, id, mid, i = -1, j = -1;
int l[301000], r[301000], arr[301000], ans;
char side;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  memset(l, -1, sizeof(l));
  memset(r, -1, sizeof(r));
  cin >> q;
  int flag = 1;
  while (q--) {
    cin >> side >> id;
    if (flag) {
      mid = id;
      flag = 0;
      continue;
    }
    if (side == 'L') {
      l[id] = ++i;
    } else if (side == 'R') {
      r[id] = ++j;
    } else {
      if (mid == id)
        cout << min(i + 1, j + 1) << endl;
      else {
        if (l[id] > -1 && i > -1) {
          ans = min(i - l[id], l[id] + j + 2);
          cout << ans << endl;
          continue;
        } else if (r[id] > -1 && j > -1) {
          ans = min(j - r[id], r[id] + i + 2);
          cout << ans << endl;
          continue;
        }
      }
    }
  }
  return 0;
}
