#include <bits/stdc++.h>
const long long infl = 1e18;
const int infi = 2e9;
const int mod = 1e9 + 7;
const int INF = int(1e9);
const int MOD = int(1e9) + 7;
const int N = 200 * 1000 + 13;
using namespace std;
long long a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  long long l = 0;
  long long r = 0;
  while (n--) {
    long long id;
    char s;
    cin >> s >> id;
    if (s == 'L') {
      a[id] = l--;
      if (r == 0) r++;
    } else if (s == 'R') {
      a[id] = r++;
      if (l == 0) l--;
    } else {
      long long x = a[id];
      long long ans = (x - l < r - x ? x - l : r - x);
      cout << ans - 1 << "\n";
    }
  }
  return 0;
}
