#include <bits/stdc++.h>
using namespace std;
const int MAXN = (int)((1e5) + 100);
int gcd(int a, int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int max(int a, int b) {
  if (a > b)
    return a;
  else
    return b;
}
int min(int a, int b) {
  if (a < b)
    return a;
  else
    return b;
}
void pr(int x) { cout << x; }
void prl(int x) { cout << x << "\n"; }
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
const int N = 200 * 1000 + 11;
void solve() {
  int q;
  cin >> q;
  std::vector<int> v(N);
  int left = 0, right = 0;
  for (int i = 0; i < q; ++i) {
    string x;
    cin >> x;
    int id;
    cin >> id;
    if (i == 0) {
      v[id] = left;
      left--;
      right++;
    } else {
      if (x == "L") {
        v[id] = left;
        left--;
      } else if (x == "R") {
        v[id] = right;
        right++;
      } else {
        cout << min(abs(v[id] - left), abs(v[id] - right)) - 1 << "\n";
      }
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  solve();
}
