#include <bits/stdc++.h>
using namespace std;
const int tinf = (int)1e9 + 7;
const long long inf = (long long)1e18 + 7;
const int N = 4e5 + 5;
long long pos[N];
int main() {
  {
    ios ::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
  }
  long long q, Max = 0, Min = 0;
  cin >> q;
  for (int i = 0; i < q; i++) {
    char c;
    long long x;
    cin >> c >> x;
    if (c == 'L') {
      if (Max == 0) {
        Max++, Min++;
        pos[x] = 1;
        continue;
      } else {
        Min--;
        pos[x] = Min;
      }
    } else if (c == 'R') {
      if (Max == 0) {
        Max++, Min++;
        pos[x] = 1;
        continue;
      } else {
        Max++;
        pos[x] = Max;
      }
    } else {
      cout << min(Max - pos[x], pos[x] - Min) << "\n";
    }
  }
}
