#include <bits/stdc++.h>
using namespace std;
int a[400005];
int main() {
  int t, l = 20001, r = 20000;
  cin >> t;
  while (t--) {
    char c;
    int n;
    cin >> c >> n;
    if (c == 'R') {
      r++;
      a[n] = r;
    }
    if (c == 'L') {
      l--;
      a[n] = l;
    }
    if (c == '?') {
      cout << min(abs(r - a[n]), abs(l - a[n])) << endl;
    }
  }
}
