#include <bits/stdc++.h>
using namespace std;
int suml[200111], sumr[200111], pos[200111], L[200111], R[200111];
int n;
void process() {
  char ch;
  int id;
  cin >> n;
  suml[0] = 0;
  sumr[0] = 0;
  for (int i = 1; i <= n; i++) {
    suml[i] = suml[i - 1];
    sumr[i] = sumr[i - 1];
    cin >> ch >> id;
    if (ch == '?')
      cout << min(suml[i] - suml[pos[id]] + L[id],
                  sumr[i] - sumr[pos[id]] + R[id])
           << endl;
    else {
      pos[id] = i;
      if (ch == 'L') {
        R[id] = sumr[i] + suml[i];
        L[id] = 0;
        suml[i]++;
      } else {
        L[id] = suml[i] + sumr[i];
        R[id] = 0;
        sumr[i]++;
      }
    }
  }
}
int main() {
  cout << setprecision(10) << fixed;
  ios::sync_with_stdio(false), cin.tie(0);
  process();
  return 0;
}
