#include <bits/stdc++.h>
using namespace std;
long long a[1000089], b[1000008];
char p[1000078];
int main() {
  long long n, i, j, k, t = 0, u = 0, l = 0, r = 0;
  cin >> n;
  for (i = 0; i < n; i++) {
    cin >> p[i] >> a[i];
  }
  for (i = 1; i < n; i++) {
    if (p[i] == 'L') {
      l--;
      b[a[i]] = l;
    } else if (p[i] == 'R') {
      r++;
      b[a[i]] = r;
    } else {
      cout << min(abs(b[a[i]] - l), abs(b[a[i]] - r)) << endl;
    }
  }
}
