#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
int main() {
  long long int ar[400010];
  long long int l = 2e5 + 2;
  long long int r = 2e5 + 2;
  long long int q;
  cin >> q;
  map<long long int, long long int> mp;
  long long int f = 1;
  while (q--) {
    char c;
    cin >> c;
    long long int n;
    cin >> n;
    if (f) {
      ar[l] = n;
      mp[n] = l;
    } else {
      if (c == 'L') {
        ar[--l] = n;
        mp[n] = l;
      } else if (c == 'R') {
        ar[++r] = n;
        mp[n] = r;
      } else {
        long long int x = mp[n];
        cout << min(abs(l - x), abs(r - x)) << "\n";
      }
    }
    f = 0;
  }
}
