#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T>
inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 5;
vector<int> x(MAXN), y(MAXN);
char s[11];
int main() {
  int cas, l = 0, r = 0, id;
  cin >> cas;
  while (cas--) {
    cin >> s >> id;
    if (s[0] == 'L')
      x[id] = ++l;
    else if (s[0] == 'R')
      y[id] = ++r;
    else if (s[0] == '?') {
      int ans;
      if (x[id] != 0)
        ans = min(l - x[id], x[id] - 1 + r);
      else
        ans = min(r - y[id], y[id] - 1 + l);
      cout << ans << endl;
    }
  }
  return 0;
}
