#include <iostream>
using namespace std;
const int N=2e5+5;
int p[N];
int n;
int main()
{
cin>>n;
int l=0,r=-1;
while(n--){
char c;
int x;
cin>>c>>x;
if(c=='L')
p[x]=--l;
else if(c=='R')
p[x]=++r;
else
cout<<min(p[x]-l,r-p[x])<<endl;;
}
//cout << "Hello world!" << endl;
return 0;
}