#include <bits/stdc++.h>
using namespace std;
long long a, b, c, d, i, e, f, g, n, m, k, l, A[200005], r;
char ch;
map<long long, long long> mp, mp1;
map<long long, char> mp2;
int main() {
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    cin >> ch >> A[i];
    if (ch == '?') {
      if (mp2[A[i]] == 'L')
        cout << min(l - mp[A[i]] - 1, mp[A[i]] + r) << endl;
      else
        cout << min(r - mp1[A[i]] - 1, mp1[A[i]] + l) << endl;
      continue;
    }
    mp[A[i]] = l;
    mp1[A[i]] = r;
    mp2[A[i]] = ch;
    if (ch == 'L') l++;
    if (ch == 'R') r++;
  }
}
