#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (a ? gcd(b % a, a) : b); }
long long power(long long a, long long n) {
  long long p = 1;
  while (n > 0) {
    if (n & 1) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
long long power(long long a, long long n, long long mod) {
  long long p = 1;
  while (n > 0) {
    if (n & 1) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& p) {
  os << "[ ";
  for (T x : p) os << x << " ";
  os << "]" << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const set<T>& p) {
  os << "{ ";
  for (T x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, const multiset<T>& p) {
  os << "{ ";
  for (T x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const map<Tk, Tv>& p) {
  os << "{ ";
  for (pair<Tk, Tv> x : p) os << x << " ";
  os << "}" << endl;
  return os;
}
template <typename Tk, typename Tv>
ostream& operator<<(ostream& os, const pair<Tk, Tv>& p) {
  os << "{" << p.first << ',' << p.second << "}";
  return os;
}
set<int> s;
map<int, int> ma;
int main() {
  int i, j;
  int q;
  scanf("%d", &q);
  while (q--) {
    char c;
    int id;
    cin >> c;
    if (c == 'L') {
      scanf("%d", &id);
      if (s.size() == 0) {
        ma[id] = 0;
        s.insert(0);
      } else {
        ma[id] = *(s.begin()) - 1;
        s.insert(ma[id]);
      }
    } else if (c == 'R') {
      scanf("%d", &id);
      if (s.size() == 0) {
        ma[id] = 0;
        s.insert(0);
      } else {
        ma[id] = *(s.rbegin()) + 1;
        s.insert(ma[id]);
      }
    } else {
      scanf("%d", &id);
      int lo = *(s.begin()), hi = *(s.rbegin());
      printf("%d\n", min(ma[id] - lo, hi - ma[id]));
    }
  }
  return 0;
}
