#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[200000];
  map<int, int> m;
  int left = 100001, right = 100000;
  for (int i = 0; i < n; i++) {
    char x;
    int y;
    cin >> x >> y;
    switch (x) {
      case 'L':
        a[--left] = y;
        m.insert(make_pair(y, left));
        break;
      case 'R':
        a[++right] = y;
        m.insert(make_pair(y, right));
        break;
      case '?':
        int mid = (right + left) / 2;
        int index = m[y];
        if (mid < index)
          cout << right - index << endl;
        else
          cout << index - left << endl;
    }
  }
}
