#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int INF = 2e18;
const int MAX = 1e5 + 3, MAX2 = 1e7 + 2;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int tc = 1;
  while (tc--) {
    int q;
    cin >> q;
    map<int, int> indx;
    int left = 0, right = 0;
    for (int j = 0; j < q; j++) {
      char ch;
      int i;
      cin >> ch >> i;
      if (ch == '?') {
        cout << min(right - indx[i], indx[i] - left) << "\n";
      } else {
        if (j == 0) {
          indx[i] = 0;
        } else if (ch == 'L') {
          left--;
          indx[i] = left;
        } else {
          right++;
          indx[i] = right;
        }
      }
    }
  }
  return 0;
}
