#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> mp;
int l, r;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int q, x;
  string str;
  l = r = 200010;
  cin >> q;
  cin >> str;
  cin >> x;
  mp[x] = l;
  while (--q) {
    cin >> str;
    cin >> x;
    if (str[0] == 'L')
      mp[x] = --l;
    else if (str[0] == 'R')
      mp[x] = ++r;
    else
      cout << min(mp[x] - l, r - mp[x]) << "\n";
  }
  return 0;
}
