#include <bits/stdc++.h>
using namespace std;
long long int vis[500005];
long long int ara[500000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int left = 200000;
  long long int right = 200001;
  long long int a, i, b;
  cin >> a;
  string s;
  for (i = 0; i < a; i++) {
    cin >> s;
    cin >> b;
    if (s[0] == 'L') {
      ara[left] = b;
      vis[b] = left;
      left--;
    } else if (s[0] == 'R') {
      ara[right] = b;
      vis[b] = right;
      right++;
    } else {
      long long int id = vis[b];
      long long int x = abs(right - id);
      x--;
      long long int y = abs(left - id);
      y--;
      cout << min(x, y) << endl;
    }
  }
  return 0;
}
