#include <bits/stdc++.h>
using namespace std;
ifstream in("input.txt");
const int mod = 1e9 + 9, SIZE = 300005;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int q;
  cin >> q;
  int size = 6e5;
  int books[size + 1];
  int lp = 3e5, rp = 3e5;
  int f = 1;
  int pos[200000 + 5] = {};
  while (q--) {
    char c;
    int id;
    cin >> c >> id;
    if (f) {
      if (c == 'L') {
        books[lp] = id;
        pos[id] = lp;
      } else {
        books[rp] = id;
        pos[id] = rp;
      }
      lp--, rp++;
      f = 0;
    } else {
      if (c == 'L') {
        books[lp] = id;
        pos[id] = lp;
        lp--;
      } else if (c == 'R') {
        pos[id] = rp;
        books[rp] = id, rp++;
      } else {
        int diff = pos[id] - lp - 1;
        int diff2 = rp - pos[id] - 1;
        int ans = min(diff, diff2);
        cout << ans << "\n";
      }
    }
  }
  return 0;
}
