#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const double PI = 3.141592653589793238463;
int freq[200];
bool comp(pair<int, int> p1, pair<int, int> p2) {
  return p1.second - p2.first < p2.second - p2.first;
}
int arr[(int)2e5 + 5];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int q;
  cin >> q;
  int l = 0, r = 0;
  while (q--) {
    char a;
    int b;
    cin >> a >> b;
    int ans = 0;
    if (a == 'L') {
      arr[b] = 0 - l;
      l++;
    } else if (a == 'R') {
      arr[b] = r + 1;
      r++;
    } else {
      cout << min(arr[b] + l - 1, r - arr[b]) << '\n';
    }
  }
  return 0;
}
