#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  while (t--) {
    long long int n, m, k, i, j, q, id, il = 1, ir = 1;
    char c;
    cin >> q;
    vector<long long int> left, right;
    vector<long long int> vl(200001), vr(200001);
    for (i = 0; i < q; i++) {
      cin >> c;
      cin >> id;
      if (c == 'L') {
        left.push_back(id);
        vl[id] = il;
        il++;
      } else if (c == 'R') {
        right.push_back(id);
        vr[id] = ir;
        ir++;
      } else {
        long long int val1, val2;
        if (vl[id] != 0) {
          val1 = left.size() - vl[id];
          val2 = right.size() + vl[id] - 1;
          cout << min(val1, val2) << endl;
        } else {
          val1 = right.size() - vr[id];
          val2 = left.size() + vr[id] - 1;
          cout << min(val1, val2) << endl;
        }
      }
    }
  }
}
