#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
vector < int > r;
vector < int > l;
map < int , char > m;
vector < int > ans;
int search_L(int x){
    for(int i=0;i<l.size();i++)
        if(l[i]==x)return l.size()-i-1;
}
int search_R(int x){
        for(int i=0;i<r.size();i++)
        if(r[i]==x)return r.size()-i-1;
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    if(chk){
        L=l.size()+(r.size()-ind-1);
        R=ind;
    }
    else
    {
        L=ind;
        R=r.size()+(l.size()-ind-1);
    }
    return min(L,R);
}
int main()
{  int q,x;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        l.push_back(x);
      if(c=='R')
        r.push_back(x);
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
vector < int > r;
vector < int > l;
map < int , char > m;
vector < int > ans;
int search_L(int x){
    for(int i=0;i<l.size();i++)
        if(l[i]==x)return i;
}
int search_R(int x){
        for(int i=0;i<l.size();i++)
        if(r[i]==x)return i;
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    if(chk){
        L=l.size()+(r.size()-ind-1);
        R=ind;
    }
    else
    {
        L=ind;
        R=r.size()+(l.size()-ind-1);
    }
    return min(L,R);
}
int main()
{  int q,x;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        l.insert(l.begin(),x);
      if(c=='R')
        r.insert(r.begin(),x);
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
map < int , int > r;
map < int , int > l;
map < int , char > m;
vector < int > ans;
int cnt_l,cnt_r;
int search_L(int x){
    //cout<<l[x]<<endl;
    return cnt_l-(l[x]+1);
}
int search_R(int x){
    //cout<<r[x]<<endl;
        return cnt_r-(r[x]+1);
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
  //cout<<cnt_l<<' '<<cnt_r<<endl;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    //cout<<ind<<endl;
    if(chk){
        L=cnt_l+ind;
        R=ind;
    //cout<<L<<' '<<R<<endl;
    }
    else
    {
        L=ind;
        R=cnt_r+l[x];
        //cout<<L<<' '<<R<<endl;
    }
    return min(L,R);
}
int main()
{  int q,x;
   cnt_l=0,cnt_r=0;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        {   l[x]=cnt_l;
            cnt_l++;
        }
      if(c=='R')
        {   r[x]=cnt_r;
            cnt_r++;
        }
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
#include <map>
using namespace std;
map < int , int > r;
map < int , int > l;
map < int , char > m;
vector < int > ans;
int cnt_l,cnt_r;
int search_L(int x){
    //cout<<l[x]<<endl;
    return cnt_l-(l[x]+1);
}
int search_R(int x){
    //cout<<r[x]<<endl;
        return cnt_r-(r[x]+1);
}
int solve(int x){
    int L,R,ind;
    bool chk=true;
  //cout<<cnt_l<<' '<<cnt_r<<endl;
    if(m[x]=='R')
        ind=search_R(x);
    if(m[x]=='L'){
        ind=search_L(x);
        chk=false;
        }
    //cout<<ind<<endl;
    if(chk){
        L=cnt_l+(cnt_r-ind-1);
        R=ind;
    //cout<<L<<' '<<R<<endl;
    }
    else
    {
        L=ind;
        R=cnt_r+l[x];
        //cout<<L<<' '<<R<<endl;
    }
    return min(L,R);
}
int main()
{  int q,x;
   cnt_l=0,cnt_r=0;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        {   l[x]=cnt_l;
            cnt_l++;
        }
      if(c=='R')
        {   r[x]=cnt_r;
            cnt_r++;
        }
      if(c=='?'){
        ans.push_back(solve(x));
        continue;
        }
    m[x]=c;
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
#include <vector>
using namespace std;
vector < int > v;
vector < int > ans;
int solve(int x){
int l,r;
for(int i=0;i<v.size();i++){
    if(v[i]==x){
        l=i;
        break;
    }}
 r=v.size()-l-1;
 return min(l,r);

}
int main()
{  int q,x;
   char c;
   cin>>q;
   while(q--){
      cin>>c>>x;
      if(c=='L')
        v.insert(v.begin(),x);
      if(c=='R')
        v.push_back(x);
      if(c=='?')
        ans.push_back(solve(x));
   }
  for(int i=0;i<ans.size();i++)
  cout<<ans[i]<<endl;
    return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=5e5;
int biggest=5e5;
map<char,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        smalest--;
        m[n]=smalest;

    }
    else if (l=='R'){
        biggest++;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-smalest,biggest-m[n]);
        cout<<endl;
    }
    }

/*
*/
return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=N;
int biggest=N;
map<int,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        m[n]=smalest--;

    }
    else if (l=='R'){
       ++ biggest;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-(smalest+1),biggest-m[n])<<endl;

    }
    }

/*
*/
return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=N;
int biggest=N;
map<char,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        m[n]=smalest--;

    }
    else if (l=='R'){
       ++ biggest;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-(smalest+1),biggest-m[n])<<endl;

    }
    }

/*
*/
return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=10;
int biggest=10;
map<char,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        m[n]=smalest--;

    }
    else if (l=='R'){
       ++ biggest;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-(smalest+1),biggest-m[n])<<endl;

    }
    }

/*
*/
return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=5e5;
int biggest=5e5;
map<char,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        --smalest;
        m[n]=smalest;

    }
    else if (l=='R'){
        biggest++;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-smalest,biggest-m[n]);
        cout<<endl;
    }
    }

/*
*/
return 0;
}


/*|In The Name of Allah|*/
/* Dear GCC compiler:
*
* If my code doesn't compile, this doesn't mean it's wrong.
* It just mean that you don't understand what I want.
* Here's my safety pig, I hope my code compile and get accepted.
* _._ _..._ .-',     _.._(`))
*'-. `     '  /-._.-'    ',/
*   )         \            '.
*  / _    _    |             \
* |  a    a    /              |
* \   .-.                     ;
*  '-('' ).-'       ,'       ;
*     '-;           |      .'
*        \           \    /
*        | 7  .__  _.-\   \
*        | |  |  ``/  /`  /
*       /,_|  |   /,_/   /
*          /,_/      '`-'
*/
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

using namespace std;
using namespace  __gnu_pbds;

long long power(long long x, long long y){
if (y == 0)
	return 1;
long long ret = power(x , y >> 1) %1000000007 ;
if (y % 2 == 0)
	return (ret * ret) % 1000000007;
else
	return (x * (ret * ret % 1000000007)) % 1000000007;
}

pair<int, int> norm(pair<int, int> p)
{
	if(p.first < 0)
	{
		p.first *= -1;
		p.second *= -1;
	}
	else if (p.first == 0 && p.second < 0)
	{
		p.second *= -1;
	}
	int d = __gcd(abs(p.first), abs(p.second));
	p.first /= d;
	p.second /= d;
	return p;
}

/*

int parent[N], sz[N];
int n, m, components;

void init(){
  iota(parent, parent+N, 0);
  fill(sz, sz+N, 1);
  components = N;
}

int find(int u){
  if(parent[u] == u)  return u;
  return parent[u] = find(parent[u]);
}

void join(int u, int v){
  u = find(u), v = find(v);
  if(u == v)  return;
  if(sz[u] < sz[v]) swap(u, v);
  parent[v] = u;
  sz[u] += sz[v];
  --components;
}
int dst[N];
int vis[N];
int parent[N];
vector<vector<pair<int,ll>>>v(N);
void dikstra(){
	set<pair<ll,ll>>s;
	clr(dst,oo);
	clr(vis,0);
	clr(parent,-1);
	dst[1]=0;
	parent[1]=-1;
	s.insert( mp(0,1 ) );
	while(!s.empty()){

		pair<ll,ll> x=*s.begin();
		s.erase(s.begin());
		ll u=x.sc;
		ll d=x.fr;
		if (vis[u])continue;
		vis[u]=1;
		for (int i=0;i<v[u].size();i++){
			ll to=v[u][i].fr;
			ll cost=v[u][i].sc;
			if (vis[to]||d+cost>=dst[to])continue;
			dst[to]=d+cost;
			parent[to]=u;
			s.insert(mp(d+cost,to));
		}
	}

return;
}
void primeFactors(int n)
{
	while (n % 2 == 0){
		n = n/2;
	}

	for (int i = 3; i <= sqrt(n); i = i + 2)
	{
		while (n % i == 0){
			cout << i << " ";
			n = n/i;
		}
	}
	if (n > 2){

	}
}

void Theaf(){
isPrime[0]=1;
isPrime[1]=1;
for (int i=2;i*i<=N;i++){
	if (isPrime[i]==1)continue;
	for (int cnt=2;i*cnt<=N;cnt++)isPrime[ cnt*i ]=1;
}

}
void dfs_EdgeClassification(int node)
{
	start[node] = timer++;

	rep(i, adj[node])
	{
		int child = adj[node][i];
		if (start[child] == -1)	// Not visited Before. Treed Edge
			dfs_EdgeClassification(child);
		else {
			if(finish[child] == -1)// then this is ancestor that called us and waiting us to finish. Then Cycle. Back Edge
				anyCycle = 1;
			else if(start[node] < start[child])	// then you are my descendant
				;	// Forward Edge
			else
				;	// Cross Edge
		}
	}

	finish[node] = timer++;
}

vectorll factorization(ll n) 	 max n is 1e12
{	// O( sqrt(n) )

	vectorll primes;

	for (ll i = 2; i  i * i <= n; ++i)	 Improve start by i = 3.
		while (n % i == 0)
			primes.push_back(i), n /= i;	 Get every prime inside n.	n  i^j	is a new number

	 Two ways to prove that n will be prime number if n  1
	 1) Think in n  pi^x as a NEW sub-problem. If we went to its sqrt and % applied, then we are facing a prime number
	 2) Prove that, for any n either all its primes before sqrt(n), or only the biggest prime after sqrt(n).
	if (n  > 1)
		primes.push_back(n);

	return primes;
}

typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;

	ordered_set X;
	X.insert(1);
	X.insert(2);
	X.insert(4);
	X.insert(8);
	X.insert(16);

	0 1 2 3 4
	1 2 4 8 16
	cout<<*X.find_by_order(1)<<endl; // 2
	cout<<*X.find_by_order(2)<<endl; // 4
	cout<<*X.find_by_order(4)<<endl; // 16
	cout<<(end(X)==X.find_by_order(6))<<endl; // true

	cout<<X.order_of_key(-5)<<endl;  // 0
	cout<<X.order_of_key(1)<<endl;   // 0
	cout<<X.order_of_key(3)<<endl;   // 2
	cout<<X.order_of_key(4)<<endl;   // 2
	cout<<X.order_of_key(400)<<endl; // 5

int t = 0;

ordered_set me;
...
me.insert({x, t++});
me.erase(me.lower_bound({x, 0}));
cout << me.order_of_key({x, 0}) << "\n";

*/
const double Eps =(1e-7);
int dcmp(double x,double y){ return fabs(x-y)<=Eps? 0: x<y ?-1:1; }
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>ordered_set;
typedef tree<pair<int, int>,null_type,less<pair<int, int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_setpair;
typedef long long  ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<pair<int,int> > vii;
typedef map<int, int> mii;
typedef map<string, int> msi;
typedef pair<int, int> pii;
#define mii map <int, int>
#define mll map <ll, ll>
#define pii pair <int, int>
#define pll pair <ll, ll>
#define vi vector <int>
#define vd vector <double>
#define vll vector <ll>
#define sll set <ll>
#define spii set <pii>
#define vpii vector <pair <int,int> >
#define vpll vector <pair <long long,long long> >
#define vvi vector <vector <int> >
#define vvpii vector <vector <pii > >
#define sfi1(v) scanf("%d",&v)
#define sfi2(v1,v2) scanf("%d %d",&v1,&v2)
#define sfi3(v1,v2,v3) scanf("%d %d %d",&v1,&v2,&v3)
#define sfll1(v) scanf("%I64d",&v);
#define sfll2(v1,v2) scanf("%I64d %I64d",&v1,&v2)
#define sfll3(v1,v2,v3) scanf("%I64d %I64d %I64d",&v1,&v2,&v3)
#define min3(a,b,c)     min(a,min(b,c))
#define max3(a,b,c)     max(a,max(b,c))
#define min4(a,b,c,d)   min(a,min(b,min(c,d)))
#define max4(a,b,c,d)   max(a,max(b,max(c,d)))
#define el	'\n'
#define lp(i,n) for(int i = 0;i < int(n);i++)
#define lp1(i,n) for(int i = 1;i <= int(n);i++)
#define rlp(i, n) for (int i = (n) - 1; i >= 0; i--)
#define rng(i,a,b) for(int i = int(a);i <= int(b);i++)
#define mp(x,y)  	make_pair((x),(y))
#define pb(x)    	push_back((x))
#define all(v)  	((v).begin()),((v).end())
#define sz(x)  		((int) (x).size())
#define bits(a) 	__builtin_popcountll(a)
#define fr first
#define sc second
#define pii pair<int, int>
#define sortva(v) sort(all(v))
#define sortvd(v) sort(v.rbegin(),v.rend())
#define sortaa(a,n) sort(a,a+n)
#define sortad(a,n) sort(a,a+n),reverse(a,a+n)
#define SS stringstream
#define ll long long
#define clr(x, y) memset(x, y, sizeof(x))
#define foreach(it,x) for(__typeof__((x).begin()) it = (x).begin(); it != (x).end(); ++it)
#define read freopen("input.in", "rt", stdin);
#define write freopen("output.in", "wt", stdout);
#define fastIO cout << fixed << setprecision(0), ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
const long long  P = (31);
const long long  key2 = 59;
int dx[] = { 0, 1, 0, -1, 1, -1, 1, -1 };
int dy[] = { 1, 0, -1, 0, -1, 1, 1, -1 };
int kx[] = { 2, 2, 1, -1, -2, -2, 1, -1 };
int ky[] = { 1, -1, 2, 2, 1, -1, -2, -2 };
ll gcd(ll a, ll b){if (a == 0)return b;return gcd(b % a, a);}
ll lcm(ll a, ll b) { return (a / gcd(a, b)) * b; }

int const N = 5e5+9, mod = 1e9+7 ,  oo = 1e9+7;

//------------------------------------------------------------------------------------

int q;
int smalest=3e5;
int biggest=3e5;
map<char,int>m;
int main()
{
    cin>>q;
    while(q--){
        char l;
    int n;
    cin>>l>>n;
    if (l=='L'){
        smalest--;
        m[n]=smalest;

    }
    else if (l=='R'){
        biggest++;
        m[n]=biggest;
    }
    else{
        cout<<min(m[n]-smalest,biggest-m[n]);
        cout<<endl;
    }
    }

/*
*/
return 0;
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define debug(i) cout << i << endl;
#define debugarr(a) for(auto i : a) cout << i << " ";

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>

using namespace std;

struct book {
    bool colocado; // direita = 1; esquerda = 0;
    int direita;
    int esquerda;
};

int main() {
    ios_base::sync_with_stdio(false);

    int q;
    cin >> q;

    map<int,book> mapa;
    int esquerda = 0;
    int direita = 0;

    while(q--) {
        char op;
        int id;
        cin >> op >> id;
        book livro;
        if(op == 'L') {
            esquerda++;
            livro.esquerda = esquerda;
            livro.colocado = 0;
            mapa[id] = livro;
        }else if(op == 'R') {
            direita++;
            livro.direita = direita;
            livro.colocado = 1;
            mapa[id] = livro;
        }else {
            livro = mapa[id];
            int esq_lv;
            int dir_lv;
            if(livro.colocado) {
                esq_lv = esquerda;
                dir_lv = abs(direita - livro.direita);
            }else {
                dir_lv = direita;
                esq_lv = abs(esquerda - livro.esquerda);
            }
            cout << min(esq_lv, dir_lv) << "\n";
        }
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define debug(i) cout << i << endl;
#define debugarr(a) for(auto i : a) cout << i << " ";

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>

using namespace std;

struct book {
    bool colocado; // direita = 1; esquerda = 0;
    ll direita;
    ll esquerda;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll q;
    cin >> q;

    map<ll,book> mapa;
    ll esquerda = 0;
    ll direita = 0;

    while(q--) {
        char op;
        ll id;
        cin >> op >> id;
        book livro;
        if(op == 'L') {
            esquerda++;
            livro.esquerda = esquerda;
            livro.direita = direita;
            livro.colocado = 0;
            mapa[id] = livro;
        }else if(op == 'R') {
            direita++;
            livro.direita = direita;
            livro.esquerda = esquerda;
            livro.colocado = 1;
            mapa[id] = livro;
        }else {
            livro = mapa[id];
            ll esq_lv;
            ll dir_lv;
            if(livro.colocado) {
                esq_lv = esquerda + livro.direita - 1;
                dir_lv = abs(direita - livro.direita);
            }else {
                dir_lv = direita + livro.esquerda - 1;
                esq_lv = abs(esquerda - livro.esquerda);
            }
            ll ans = min(esq_lv, dir_lv);
            cout << ans << "\n";
        }
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define debug(i) cout << i << endl;
#define debugarr(a) for(auto i : a) cout << i << " ";

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>

using namespace std;

struct book {
    bool colocado; // direita = 1; esquerda = 0;
    int direita;
    int esquerda;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int q;
    cin >> q;

    map<int,book> mapa;
    int esquerda = 0;
    int direita = 0;

    while(q--) {
        char op;
        int id;
        cin >> op >> id;
        book livro;
        if(op == 'L') {
            esquerda++;
            livro.esquerda = esquerda;
            livro.direita = direita;
            livro.colocado = 0;
            mapa[id] = livro;
        }else if(op == 'R') {
            direita++;
            livro.esquerda = esquerda;
            livro.direita = direita;
            livro.colocado = 1;
            mapa[id] = livro;
        }else {
            livro = mapa[id];
            int esq_lv;
            int dir_lv;
            if(livro.colocado) {
                esq_lv = esquerda;
                dir_lv = direita - livro.direita;
            }else {
                dir_lv = direita;
                esq_lv = esquerda - livro.esquerda;
            }
            cout << min(esq_lv, dir_lv) << "\n";
        }
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define debug(i) cout << i << endl;
#define debugarr(a) for(auto i : a) cout << i << " ";

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>

using namespace std;

struct book {
    bool colocado; // direita = 1; esquerda = 0;
    ll direita;
    ll esquerda;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll q;
    cin >> q;

    map<ll,book> mapa;
    ll esquerda = 0;
    ll direita = 0;

    while(q--) {
        char op;
        ll id;
        cin >> op >> id;
        book livro;
        if(op == 'L') {
            esquerda++;
            livro.esquerda = esquerda;
            livro.colocado = 0;
            mapa[id] = livro;
        }else if(op == 'R') {
            direita++;
            livro.direita = direita;
            livro.colocado = 1;
            mapa[id] = livro;
        }else {
            livro = mapa[id];
            ll esq_lv;
            ll dir_lv;
            if(livro.colocado) {
                esq_lv = esquerda;
                dir_lv = abs(direita - livro.direita);
            }else {
                dir_lv = direita;
                esq_lv = abs(esquerda - livro.esquerda);
            }
            ll ans = min(esq_lv, dir_lv);
            cout << ans << "\n";
        }
    }
}


#include <bits/stdc++.h>

#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i,a,b) for (int i = a; i <= b; i++)
#define debug(i) cout << i << endl;
#define debugarr(a) for(auto i : a) cout << i << " ";

#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define pi pair<int,int>
#define pll pair<ll, ll>
#define vpi vector<pi>

using namespace std;

struct book {
    bool colocado; // direita = 1; esquerda = 0;
    ll direita;
    ll esquerda;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll q;
    cin >> q;

    map<ll,book> mapa;
    ll esquerda = 0;
    ll direita = 0;

    while(q--) {
        char op;
        ll id;
        cin >> op >> id;
        book livro;
        if(op == 'L') {
            esquerda++;
            livro.esquerda = esquerda;
            livro.direita = direita;
            livro.colocado = 0;
            mapa[id] = livro;
        }else if(op == 'R') {
            direita++;
            livro.direita = direita;
            livro.esquerda = esquerda;
            livro.colocado = 1;
            mapa[id] = livro;
        }else {
            livro = mapa[id];
            ll esq_lv;
            ll dir_lv;
            if(livro.colocado) {
                esq_lv = esquerda + livro.direita;
                dir_lv = abs(direita - livro.direita);
            }else {
                dir_lv = direita + livro.esquerda;
                esq_lv = abs(esquerda - livro.esquerda);
            }
            ll ans = min(esq_lv, dir_lv);
            cout << ans << "\n";
        }
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep(i, a, b) for(ll i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
map<int,int> map1;
int s[mm];

int main()
{
    fast_io;
    int cnt=1,q;
    cin>>q;
    while(q--)
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            s[cnt]=1;
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id])
            {
                int id1=lower_bound(all(l),id+1)-l.begin();
                cout<<sz(l)-id1<<"\n";
            }
            else
            {
                int id2=lower_bound(all(r),id+1)-r.begin();
                cout<<sz(r)-id2<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep(i, a, b) for(ll i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
map<int,int> map1;

int main()
{
    fast_io;
    int cnt=1;
    cases
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
            int id1=upper_bound(all(l),id)-l.begin();
            int id2=upper_bound(all(r),id)-r.begin();
           // cout<<id1<<" "<<id2<<"\n";
            if(sz(l)>0&&id1>0&&l[id1-1]==id)
            {
                cout<<sz(l)-id1<<"\n";
            }
            else
            {
                cout<<sz(r)-id2<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep(i, a, b) for(ll i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
map<int,int> map1;
int s[mm];

int main()
{
    fast_io;
    int cnt=1;
    cases
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            s[cnt]=1;
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id])
            {
                int id1=upper_bound(all(l),id)-l.begin();
                cout<<sz(l)-id1<<"\n";
            }
            else
            {
                int id2=upper_bound(all(r),id)-r.begin();
                cout<<sz(r)-id2<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define rep(i, a, b) for(int i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
int map1[mm];
int s[mm];

int main()
{
    fast_io;
    int cnt=1,q;
    cin>>q;
    while(q--)
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            s[cnt]=1;
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id])
            {
                int id1=lower_bound(all(l),id+1)-l.begin();
                int ans=sz(l)-id1;
                cout<<ans<<"\n";
            }
            else
            {
                int id2=lower_bound(all(r),id+1)-r.begin();
                int ans=sz(r)-id2;
                cout<<ans<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep(i, a, b) for(ll i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
map<int,int> map1;

int main()
{
    fast_io;
    int cnt=1;
    cases
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
            int id1=upper_bound(all(l),id)-l.begin();
            int id2=upper_bound(all(r),id)-r.begin();
           // cout<<id1<<" "<<id2<<"\n";
            if(l[id1-1]==id)
            {
                cout<<sz(l)-id1<<"\n";
            }
            else
            {
                cout<<sz(r)-id2<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 500005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define rep(i, a, b) for(int i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
//vector<int> l,r;
int l[mm],r[mm];
int s[mm];

int main()
{
    fast_io;
    int cnt1=1,cnt2=1,q;
    cin>>q;
    while(q--)
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            s[id]=1;
            l[id]=cnt1++;
        }
        else if(ch=='R')
        {
            s[id]=2;
            r[id]=cnt2++;
        }
        else
        {
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id]==1)
            {
                cout<<min(cnt1-1-l[id],cnt2+l[id]-2)<<"\n";
            }
            else
            {
                cout<<min(cnt2-1-r[id],cnt1+r[id]-2)<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 300005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (ll)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(ll i = 0; i < ll(n); ++i)
#define rep(i, a, b) for(ll i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
vector<int> l,r;
map<int,int> map1;
int s[mm];

int main()
{
    fast_io;
    int cnt=1;
    cases
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            map1[id]=cnt;
            l.pb(cnt);
            s[cnt]=1;
            cnt++;
        }
        else if(ch=='R')
        {
            map1[id]=cnt;
            r.pb(cnt);
            cnt++;
        }
        else
        {
            id=map1[id];
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id])
            {
                int id1=upper_bound(all(l),id-1)-l.begin();
                cout<<sz(l)-id1-1<<"\n";
            }
            else
            {
                int id2=upper_bound(all(r),id-1)-r.begin();
                cout<<sz(r)-id2-1<<"\n";
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int    ll;
typedef long double    ld;
typedef pair<ll,ll>   ii;
typedef vector< ii >   vii;
typedef vector<int>     vi;
typedef vector< vi >   vvi;

#define mm 500005
#define nn 5008
#define xx real()
#define yy imag()
#define mod 998244353
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define sz(a) (int)(a.size())
#define all(a) a.begin(),a.end()
#define forn(i, n) for(int i = 0; i < int(n); ++i)
#define rep(i, a, b) for(int i = ll(a); i <= ll(b); ++i)
#define cases ll t;  cin>>t;   while(t--)
#define chck(a,n) forn(iiii,ll(n)) cout<<ll(a[iiii])<<" "; cout<<endl
#define fast_io ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
//vector<int> l,r;
int l[mm],r[mm];
int s[mm];

int main()
{
    fast_io;
    int cnt1=1,cnt2=1,q;
    cin>>q;
    while(q--)
    {
        char ch;
        int id;
        cin>>ch>>id;
        if(ch=='L')
        {
            s[id]=1;
            l[id]=cnt1++;
        }
        else if(ch=='R')
        {
            s[id]=2;
            r[id]=cnt2++;
        }
        else
        {
           // cout<<id1<<" "<<id2<<"\n";
            if(s[id]==1)
            {
                cout<<cnt1-1-l[id]<<"\n";
            }
            else
            {
                cout<<cnt2-1-r[id]<<"\n";
            }
        }
    }
    return 0;
}


//17571@nith.ac.in (mail address) @Ankit Verma
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define debug(x) cout <<#x<<" "<<endl;
#define pb push_back
#define MOD 1000000007
#define MOD9 1000000009
#define ll long long
#define gc getchar_unlocked
#define eps 1e-8
#define ms(s,n) memset(s,n,sizeof(s))
#define bolt ios::sync_with_stdio(false)
const double pie = 2 * acos(0.0);
#define prec(n) fixed<<setprecision(n)
#define eof (scanf("%d",&n))!=EOF

int main()
{
    bolt; // *TeZ*
    cin.tie(NULL);
    cout.tie(NULL);
	ll tt; cin >> tt;
     int r = 0; int l = 0;  unordered_map <int,int> m;
	while(tt--) {
		char pos; int id;
		cin >> pos; cin >> id;
	     if(pos == 'R') {
	    	r++;
	    	m[id] = r;
	    }
	    else if(pos == 'L') {
	    	l--;
	    	m[id] = l;
	    }
	    else {
	    	if(m[id] > 0) {
               int x = abs(m[id] - l) - 1;
               int y = abs(r - m[id]);
               cout << min(x, y) << endl;
	    	}
	    	else {
	    		int x = abs(r - l) - 1;
	    		int y = abs(m[id] - l);
	    		cout << min(x, y) << endl;
	    	}
	    }
	}

     return 0;
}


//17571@nith.ac.in (mail address) @Ankit Verma
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define debug(x) cout <<#x<<" "<<endl;
#define pb push_back
#define MOD 1000000007
#define MOD9 1000000009
#define ll long long
#define gc getchar_unlocked
#define eps 1e-8
#define ms(s,n) memset(s,n,sizeof(s))
#define bolt ios::sync_with_stdio(false)
const double pie = 2 * acos(0.0);
#define prec(n) fixed<<setprecision(n)
#define eof (scanf("%d",&n))!=EOF

int main()
{
    bolt; // *TeZ*
    cin.tie(NULL);
    cout.tie(NULL);
	ll tt; cin >> tt;
     int r = 0; int l = 0;  unordered_map <int,int> m;
	while(tt--) {
		char pos; int id;
		cin >> pos; cin >> id;
	     if(pos == 'R') {
	    	m[id] = r;
	    	r++;
	    }
	    else if(pos == 'L') {
	    	m[id] = l;
	    	l--;
	    }
        else {
	    int x = m[id] - l - 1; int y = r - m[id] - 1;
	    cout << min(x, y) << endl;
        }
	 }

     return 0;
}


//17571@nith.ac.in (mail address) @Ankit Verma
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define debug(x) cout <<#x<<" "<<endl;
#define pb push_back
#define MOD 1000000007
#define MOD9 1000000009
#define ll long long
#define gc getchar_unlocked
#define eps 1e-8
#define ms(s,n) memset(s,n,sizeof(s))
#define bolt ios::sync_with_stdio(false)
const double pie = 2 * acos(0.0);
#define prec(n) fixed<<setprecision(n)

int main()
{
    bolt; // *TeZ*
    cin.tie(NULL);
    cout.tie(NULL);
	ll tt; cin >> tt;
     int r = 1; int l = 0;  unordered_map <int,int> m;
	while(tt--) {
		char pos; int id;
		cin >> pos; cin >> id;
	     if(pos == 'R') {
	    	m[id] = r;
	    	++r;
	    }
	    else if(pos == 'L') {
	    	m[id] = l;
	    	--l;
	    }
        else {
	    int x = m[id] - l - 1; int y = r - m[id] - 1;
	    cout << min(x, y) << endl;
        }
	 }

     return 0;
}


//17571@nith.ac.in (mail address) @Ankit Verma
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define debug(x) cout <<#x<<" "<<endl;
#define pb push_back
#define MOD 1000000007
#define MOD9 1000000009
#define ll long long
#define gc getchar_unlocked
#define eps 1e-8
#define ms(s,n) memset(s,n,sizeof(s))
#define bolt ios::sync_with_stdio(false)
const double pie = 2 * acos(0.0);
#define prec(n) fixed<<setprecision(n)
#define eof (scanf("%d",&n))!=EOF

int main()
{
    bolt; // *TeZ*
    cin.tie(NULL);
    cout.tie(NULL);
	ll tt; cin >> tt;
     int r = 0; int l = 0;  unordered_map <int,int> m;
	while(tt--) {
		char pos; int id;
		cin >> pos; cin >> id;
	     if(pos == 'R') {
	    	r++;
	    	m[id] = r;
	    }
	    else if(pos == 'L') {
	    	l--;
	    	m[id] = l;
	    }
	    else {
	    	if(m[id] > 0) {
               int x = abs(m[id] - l - 1);
               int y = abs(r - m[id]);
               cout << min(x, y) << endl;
	    	}
	    	else {
	    		int x = abs(r - l - 1);
	    		int y = abs(m[id] - l);
	    		cout << min(x, y) << endl;
	    	}
	    }
	}

     return 0;
}


//17571@nith.ac.in (mail address) @Ankit Verma
#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#define debug(x) cout <<#x<<" "<<endl;
#define pb push_back
#define MOD 1000000007
#define MOD9 1000000009
#define ll long long
#define gc getchar_unlocked
#define eps 1e-8
#define ms(s,n) memset(s,n,sizeof(s))
#define bolt ios::sync_with_stdio(false)
const double pie = 2 * acos(0.0);
#define prec(n) fixed<<setprecision(n)
#define eof (scanf("%d",&n))!=EOF

int main()
{
    bolt; // *TeZ*
    cin.tie(NULL);
    cout.tie(NULL);
	ll tt; cin >> tt;
     int r = 1; int l = 0;  unordered_map <int,int> m;
	while(tt--) {
		char pos; int id;
		cin >> pos; cin >> id;
	     if(pos == 'R') {
	    	m[id] = r;
	    	++r;
	    }
	    else if(pos == 'L') {
	    	m[id] = l;
	    	--l;
	    }
        else {
	    int x = m[id] - l - 1; int y = r - m[id] - 1;
	    cout << min(x, y) << endl;
        }
	 }

     return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k,x,y;
	char rt;
	cin>>n;
	vector<int> v;
	cin>>rt>>k;
	v.push_back(k);
	n--;
	while(n--)
	{
		char a;
		cin>>a>>k;
		if(a=='L')
		{
			v.insert(v.begin(),k);
		}
		else if(a=='R')
		{
			v.push_back(k);
		}
		else
		{

			auto it=find(v.begin(),v.end(),k);

				//cout<<" q";
				x=it-v.begin();
				y=v.end()-it-1;
				if(x>y) cout<<y;
				else cout<<x;
				cout<<endl;

		}

	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k,x,y;
	char rt;
	cin>>n;
	vector<int> v;
	cin>>rt>>k;
	v.push_back(k);
	n--;
	while(n--)
	{
		char a;
		cin>>a>>k;
		if(a=='L')
		{
			v.insert(v.begin(),k);
		}
		else if(a=='R')
		{
			v.push_back(k);
		}
		else
		{
			x=find(v.begin(),v.end(),k)-v.begin();
			y=v.end()-find(v.begin(),v.end(),k);
			if(x>y)
			cout<<y;
			else cout<<x;
			cout<<endl;
		}
	}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define sd second
#define max 100000
int min(int a, int b)
{
	if (a>b) return b;
	else return a;
}
int main()
{
	int v[2*max];
	char d;
	int x,q,rend=-1,lend=1;
	cin>>q;
	bool k=false;
	while(q--)
	{
		cin>>d>>x;
		if(d=='L')
		{
			lend--;
			v[x]=lend;
			if(!k){
				v[x]=0;
				rend=0;
				k=true;
			}

		}
		else if(d=='R')
		{
			rend++;
			v[x]=rend;
			if(!k){
				v[x]=0;
				lend=0;
				k=true;
			}

		}
		else
		{
			cout<<min(rend-v[x],v[x]-lend);
			cout<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define f first
#define sd second
#define max 100000
int min(int a, int b)
{
	if (a>b) return b;
	else return a;
}
int main()
{
	int v[2*max];
	char d;
	int x,q,rend=-1,lend=1;
	cin>>q;
	bool k=false;
	while(q--)
	{
		cin>>d>>x;
		if(d=='L')
		{
			lend--;
			v[x]=lend;
			if(!k){
				rend=0;
				k=true;
			}

		}
		else if(d=='R')
		{
			rend++;
			v[x]=rend;
			if(!k){
				lend=0;
				k=true;
			}

		}
		else
		{
			cout<<min(rend-v[x],v[x]-lend);
			cout<<endl;
		}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
	int n,k,x,y;
	char rt;
	cin>>n;
	vector<int> v;
	cin>>rt>>k;
	v.push_back(k);
	n--;
	while(n--)
	{
		char a;
		cin>>a>>k;
		if(a=='L')
		{
			v.insert(v.begin(),k);
		}
		else if(a=='R')
		{
			v.push_back(k);
		}
		else
		{

			auto it=find(v.begin(),v.end(),k);

				//cout<<" q";
				x=it-v.begin();
				y=v.end()-it-1;
				if(x>y) cout<<y;
				else cout<<x;
				cout<<endl;

		}

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2 * 1e5 + 20;

map<int, int> m;

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;

    long long indL = -1, indR = 1;

    char q;
    long long id;

    cin >> q >> id;

    if (q == 'L') {
        m[id] = 0;
    }
    else if (q == 'R') {
        m[id] = 0;
    }
    t--;

    while (t--) {
        cin >> q >> id;
        if (q == 'L') {
            m[id] = indL;
            indL--;
        }
        else if (q == 'R') {
            m[id] = indR;
            indR++;
        }
        else {
            int ind = m[id];
            cout << min(ind - indL - 1, indR - ind - 1) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2 * 1e5 + 20;

int m[N];

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;

    int indL = 0, indR = 0;

    while (t--) {
        char q;
        int id;
        cin >> q >> id;
        if (q == 'L') {
            indL--;
            m[id] = indL;
        }
        else if (q == 'R') {
            indR++;
            m[id] = indR;
        }
        else {
            int ind = m[id];
            cout << min(ind - indL, indR - ind) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

map<int, int> m;

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;

    int indL = 0, indR = 0;

    while (t--) {
        char q;
        int id;
        cin >> q >> id;
        if (q == 'L') {
            indL--;
            m[id] = indL;
        }
        else if (q == 'R') {
            indR++;
            m[id] = indR;
        }
        else {
            int ind = m[id];
            cout << min(ind - indL, indR - ind) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

list<pair<pair<int, int>,int>> l;

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        char q;
        int id;
        cin >> q >> id;
        if (q == 'L') {
            l.push_front(make_pair(make_pair(0, l.size()),id));
            auto it = l.begin();
            it++;
            while (it != l.end()) {
                it -> first.first++;
                it++;
            }
        }
        else if (q == 'R') {
            l.push_back(make_pair(make_pair(l.size(), 0),id));
            auto it = l.begin();
            auto e = l.end();
            e--;
            while (it != e) {
                it -> first.second++;
                it++;
            }
        }
        else {
            for (const auto it : l) {
                if (it.second == id)
                    cout << min(it.first.first, it.first.second) << "\n";
            }
        }
    }

    // cout << endl << endl;

    // for(auto it : l) {
    //     cout << it.first.first << " " << it.first.second << " " << it.second << "\n";
    // }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2 * 1e5 + 20;

long long m[N];

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;

    long long indL = -1, indR = 1;

    char q;
    long long id;

    cin >> q >> id;

    if (q == 'L') {
        m[id] = 0;
    }
    else if (q == 'R') {
        m[id] = 0;
    }
    t--;

    while (t--) {
        cin >> q >> id;
        if (q == 'L') {
            m[id] = indL;
            indL--;
        }
        else if (q == 'R') {
            m[id] = indR;
            indR++;
        }
        else {
            int ind = m[id];
            cout << min(ind - indL - 1, indR - ind - 1) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 2 * 1e5 + 20;

long long m[N];

int main () {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;

        long long indL = 0, indR = 0;

    while (t--) {
        char q;
        long long id;
        cin >> q >> id;
        if (q == 'L') {
            indL--;
            m[id] = indL;
        }
        else if (q == 'R') {
            indR++;
            m[id] = indR;
        }
        else {
            int ind = m[id];
            cout << min(ind - indL, indR - ind) << "\n";
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#include<iostream>
#define ll long long int
#define INF 2018012016
#define FOR(n) for(ll d = 0; d < n; d++)
#define FORD(n) for(ll d = n-1; d >= 0; d--)
using namespace std;
ll mat[200000];
ll LN[200000];
ll RN[200000];
int main()
{
    ll n, l;
    cin>>n;
    ll lkol = 0;
    ll rkol = 0;
    for(int d = 0; d < n; d++)
    {
        char c;
        cin>>c;
        ll a;
        cin>>a;
        if(c == 'L')
        {
            lkol++;
            LN[a] = lkol;
        }
        if(c == 'R')
        {
            rkol++;
            RN[a] = rkol;
        }
        if(c == '?')
        {
            cout<<min(lkol - LN[a], rkol - RN[a])<<endl;
        }
    }
}


#include <bits/stdc++.h>
#include<iostream>
#define ll long long int
#define INF 2018012016
#define FOR(n) for(ll d = 0; d < n; d++)
#define FORD(n) for(ll d = n-1; d >= 0; d--)
using namespace std;
ll mat[200010];
ll LN[200010];
ll RN[200010];
int main()
{
    ll n, l;
    cin>>n;
    ll lkol = 0;
    ll rkol = 0;
    for(int d = 0; d < n; d++)
    {
        LN[d] = INF;
        RN[d] = INF;
    }
    for(int d = 0; d < n; d++)
    {
        char c;
        cin>>c;
        ll a;
        cin>>a;
        if(c == 'L')
        {
            lkol++;
            LN[a] = lkol;
        }
        if(c == 'R')
        {
            rkol++;
            RN[a] = rkol;
        }
        if(c == '?')
        {
            cout<<min(lkol - LN[a], rkol - RN[a])<<endl;
        }
    }
}


#include <bits/stdc++.h>
#include<iostream>
#define ll long long int
#define INF 2018012016
#define FOR(n) for(ll d = 0; d < n; d++)
#define FORD(n) for(ll d = n-1; d >= 0; d--)
using namespace std;
ll mat[200010];
ll LN[200010];
int main()
{
    ll n;
    cin>>n;
    ll l = 0, r = 0;
    for(int d = 0; d < n; d++)
    {
        char c;
        cin>>c;
        ll a;
        cin>>a;
        if(c == 'L')
        {
            l--;
            LN[a] = l;
        }
        if(c == 'R')
        {
            r++;
            LN[a] = r;
        }
        if(c == '?')
        {
            cout<<min(abs(l - LN[a]), abs(r - LN[a]))<<endl;
        }
    }
}


#include <bits/stdc++.h>
#include<iostream>
#define ll long long int
#define INF 2018012016
#define FOR(n) for(ll d = 0; d < n; d++)
#define FORD(n) for(ll d = n-1; d >= 0; d--)
using namespace std;
ll mat[200010];
ll LN[200010];
int main()
{
    ll n;
    cin>>n;
    ll l = 1, r = 0;
    for(int d = 0; d < n; d++)
    {
        char c;
        cin>>c;
        ll a;
        cin>>a;
        if(c == 'L')
        {
            l--;
            LN[a] = l;
        }
        if(c == 'R')
        {
            r++;
            LN[a] = r;
        }
        if(c == '?')
        {
            cout<<min(abs(l - LN[a]), abs(r - LN[a]))<<endl;
        }
    }
}


#include <bits/stdc++.h>
#include<iostream>
#define ll long long int
#define INF 2018012016
#define FOR(n) for(ll d = 0; d < n; d++)
#define FORD(n) for(ll d = n-1; d >= 0; d--)
using namespace std;
ll mat[200010];
ll LN[200010];
ll RN[200010];
int main()
{
    ll n, l;
    cin>>n;
    ll lkol = 0;
    ll rkol = 0;
    for(int d = 0; d < n; d++)
    {
        char c;
        cin>>c;
        ll a;
        cin>>a;
        if(c == 'L')
        {
            lkol++;
            LN[a] = lkol;
        }
        if(c == 'R')
        {
            rkol++;
            RN[a] = rkol;
        }
        if(c == '?')
        {
            cout<<min(lkol - LN[a], rkol - RN[a])<<endl;
        }
    }
}


#include<iostream>
#include<algorithm>
using namespace std;
int f[200005],ar[200005];
int main(){
	int t,sum=0,idx1=0,idx2=0,que,i,j;
	char x;
	cin>>t;
	while(t--){
		cin>>x>>que;
		if(x=='R'){				//dung
			ar[idx2+1]=que;
			idx2++;
			f[que]=sum;
			sum++;
		}
		if(x=='L'){
			for(i=1;i<=idx2;i++){
				f[ar[i]]++;
			}
			ar[idx2+1]=que;
			idx2++;
			f[que]=0;
			sum++;
		}
		if(x=='?'){
			cout<<min(f[que],sum-f[que]-1)<<endl;
		}
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
int f[10005],ar[10005];
int main(){
	int t,sum=0,idx1=0,idx2=0,que,i,j;
	char x;
	cin>>t;
	while(t--){
		cin>>x>>que;
		if(x=='R'){				//dung
			ar[idx2+1]=que;
			idx2++;
			f[que]=sum;
			sum++;
		}
		if(x=='L'){
			for(i=1;i<=idx2;i++){
				f[ar[i]]++;
			}
			ar[idx2+1]=que;
			idx2++;
			f[que]=0;
			sum++;
		}
		if(x=='?'){
			cout<<min(f[que],sum-f[que]-1)<<endl;
		}
	}
}


#include<iostream>
using namespace std;
char bla[200005];
int rvoid[200005],lvoid[200005],allvoid[200005];
int main(){
	int t,sum=0,bfque,que,i;
	bool first=1;
	char x;
	cin>>t;
	i=0;
	while(t--){
		cin>>x;
		cin>>que;
		if(x=='R'){				//dung
			allvoid[que]=allvoid[bfque]+1;
			if(!first){
				rvoid[que]=rvoid[bfque]+1;
				lvoid[que]=lvoid[bfque];
			}
			else{
				first=0;
			}
			bfque=que;
			bla[que]='R';
		}
		if(x=='L'){
			allvoid[que]=allvoid[bfque]+1;
			if(!first){
				rvoid[que]=rvoid[bfque];
				lvoid[que]=lvoid[bfque]+1;
			}
			else{
				first=0;
			}
			bfque=que;
			bla[que]='L';
		}
		if(x=='?'){
			if(bla[que]=='R'){
				cout<<min(rvoid[bfque]-rvoid[que],lvoid[bfque]+allvoid[que]-lvoid[que]);
				cout<<endl;
			}
			else{
				cout<<min(lvoid[bfque]-lvoid[que],rvoid[bfque]+allvoid[que]-rvoid[que]);
				cout<<endl;
			}
		}
	}
}


#include<iostream>
using namespace std;
char bla[200005];
int rvoid[200005],lvoid[200005],allvoid[200005];
int main(){
	int t,sum=0,bfque,que,i;
	bool first=1;
	char x;
	cin>>t;
	i=0;
	while(t--){
		cin>>x;
		cin>>que;
		if(x=='R'){				//dung
			if(!first){
				allvoid[que]=allvoid[bfque]+1;
				rvoid[que]=rvoid[bfque]+1;
				lvoid[que]=lvoid[bfque];
			}
			else{
				first=0;
			}
			bfque=que;
			bla[que]='R';
		}
		if(x=='L'){
			if(!first){
				allvoid[que]=allvoid[bfque]+1;
				rvoid[que]=rvoid[bfque];
				lvoid[que]=lvoid[bfque]+1;
			}
			else{
				first=0;
			}
			bfque=que;
			bla[que]='L';
		}
		if(x=='?'){
			if(bla[que]=='R'){
				cout<<min(rvoid[bfque]-rvoid[que],lvoid[bfque]+allvoid[que]-lvoid[que]);
				cout<<endl;
			}
			else{
				cout<<min(lvoid[bfque]-lvoid[que],rvoid[bfque]+allvoid[que]-rvoid[que]);
				cout<<endl;
			}
		}
	}
}


#include<iostream>
#include<algorithm>
using namespace std;
int f[200005],ar[200005];
int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t,sum=0,idx1=0,idx2=0,que,i,j;
	char x;
	cin>>t;
	while(t--){
		cin>>x>>que;
		if(x=='R'){				//dung
			ar[idx2+1]=que;
			idx2++;
			f[que]=sum;
			sum++;
		}
		if(x=='L'){
			for(i=1;i<=idx2;i++){
				f[ar[i]]++;
			}
			ar[idx2+1]=que;
			idx2++;
			f[que]=0;
			sum++;
		}
		if(x=='?'){
			cout<<min(f[que],sum-f[que]-1)<<endl;
		}
	}
}


#include<iostream>
using namespace std;
char bla[200005];
int rvoid[200005],lvoid[200005],allvoid[200005];
int main(){
	int t,sum=0,bfque,que,i;
	char x;
	cin>>t;
	i=0;
	while(t--){
		cin>>x;
		cin>>que;
		if(x=='R'){				//dung
			allvoid[que]=allvoid[bfque]+1;
			rvoid[que]=rvoid[bfque]+1;
			lvoid[que]=lvoid[bfque];
			bfque=que;
			bla[que]='R';
		}
		if(x=='L'){
			allvoid[que]=allvoid[bfque]+1;
			rvoid[que]=rvoid[bfque];
			lvoid[que]=lvoid[bfque]+1;
			bfque=que;
			bla[que]='L';
		}
		if(x=='?'){
			if(bla[que]=='R'){
				cout<<min(rvoid[bfque]-rvoid[que],lvoid[bfque]+allvoid[que]-lvoid[que]);
				cout<<endl;
			}
			else{
				cout<<min(lvoid[bfque]-lvoid[que],rvoid[bfque]+allvoid[que]-rvoid[que]);
				cout<<endl;
			}
		}
	}
}


#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
    int L = -1;
    int R = -1;
};
struct Node node[200002];
int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    int left = 0, right = 0;
    memset(node, -1, sizeof(struct Node)*200001);
    while(q--)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(s == "L")
        {
            if(left > 0)
            {
                left++;
                node[n] = (struct Node){.L = left, .R = 0};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else if(s == "R")
        {
            if(right > 0)
            {
                right++;
                node[n] = (struct Node){.L = 0, .R = right};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else
        {
//            cout << "left:" << left << ", L=" << node[n].L << endl;
//            cout << "right:" << right << ", R=" << node[n].R << endl;
            int a = std::min(left + node[n].R, right - node[n].R);
            int b = std::min(left - node[n].L, right + node[n].L);
//            cout << "a=" << a << ", b=" << b << endl;
            cout << std::min(a, b) << endl;
            //cout << std::min(left - node[n].L, right - node[n].R) << endl;
        }
    }
}


#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
    int L = -1;
    int R = -1;
};
struct Node node[200002];
int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    int left = 0, right = 0;
    memset(node, -1, sizeof(struct Node)*200001);
    while(q--)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(s == "L")
        {
            if(left > 0)
            {
                left++;
                node[n] = (struct Node){.L = left, .R = 0};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else if(s == "R")
        {
            if(right > 0)
            {
                right++;
                node[n] = (struct Node){.L = 0, .R = right};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else
        {
            int a = 1e7;
            if(node[n].R > 0)
                a = std::min(a, std::min(left+node[n].R, right-node[n].R));
            if(node[n].L > 0)
                a = std::min(a, std::min(left-node[n].L, right+node[n].L));
            cout << a << endl;
        }
    }
}


#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int node[200002];
int main()
{
#ifdef LOCAL
	freopen("test.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    int left = 0, right = 0;
    memset(node, 0, sizeof(int)*200002);
    for(int i = 0;i < q; i++)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(i == 0)
        {
            node[n] = ++left;
            right++;
            continue;
        }
        if(s == "L")
            node[n] = ++left;
        else if(s == "R")
            node[n] = ++right;
        else
        {
            int ans = std::min(abs(node[n]-left), abs(node[n]-right));
            cout << ans << endl;
        }
    }
}


#include <iostream>
#include <list>
#include <iterator>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    std::list<int> mylist;
    while(q--)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(s == "L")
            mylist.push_front(n);
        else if(s == "R")
            mylist.push_back(n);
        else
        {
            int ans1 = 0;
            for(auto it = mylist.begin(); it != mylist.end(); it++)
            {
                if(*it == n)
                {
                    ans1 = std::distance(it, mylist.begin());
                    break;
                }
            }
            int ans2 = 0;
            for(auto it = mylist.rbegin(); it != mylist.rend(); it++)
            {
                if(*it == n)
                {
                    ans2 = std::distance(it, mylist.rbegin());
                    break;
                }
            }
            //cout << "ans1=" << ans1 << " ans2=" << ans2 << endl;
            cout << std::max(0, std::min(ans1-2, ans2-2)) << endl;
        }
    }
}


#include <iostream>
#include <cstring>
using namespace std;
struct Node
{
    int L = -1;
    int R = -1;
};
struct Node node[200002];
int main()
{
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    int left = 0, right = 0;
    memset(node, -1, sizeof(struct Node)*200001);
    while(q--)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(s == "L")
        {
            if(left > 0)
            {
                left++;
                node[n] = (struct Node){.L = left, .R = 0};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else if(s == "R")
        {
            if(right > 0)
            {
                right++;
                node[n] = (struct Node){.L = 0, .R = right};
            }
            else
            {
                left++, right++;
                node[n] = (struct Node){.L = left, .R = right};
            }
        }
        else
        {
//            cout << "left:" << left << ", L=" << node[n].L << endl;
//            cout << "right:" << right << ", R=" << node[n].R << endl;
            cout << std::min(left - node[n].L, right- node[n].R) << endl;
        }
    }
}


#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
int node[200002];
int main()
{
#ifdef LOCAL
	freopen("test.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);
    int q;
    cin >> q;
    int left = 0, right = 0;
    memset(node, 0, sizeof(int)*200002);
    for(int i = 0;i < q; i++)
    {
        string s;
        int n;
        cin >> s;
        cin >> n;
        if(i == 0)
        {
            node[n] = left--;
            right++;
            continue;
        }
        if(s == "L")
            node[n] = left--;
        else if(s == "R")
            node[n] = right++;
        else
        {
//            cout << "node=" << node[n] << ", left=" << left << ", right" << right << endl;
            int ans = std::min(abs(node[n]-left), abs(node[n]-right));
            cout << ans-1 << endl;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;
typedef long int LI;

int main() {
  int q;
  cin >> q;
  unordered_map<int, int> mp;
  unordered_map<int, int> :: iterator it;
  while(q--) { //O(2*1e5)
    char t;
    int num;
    cin >> t >> num;
    if(t == 'L') {
      mp[num] = 0;
      for(it=next(mp.begin()); it!=mp.end(); it++) { //O(2*1e5)) --> when all queries are 'L'
        mp[it->first]++;
      }
    } else if (t=='R') { //O(1)
      mp[num] = mp.size();
    } else if (t=='?') { //O(1)
      if(mp[num] < mp.size()-mp[num]-1) {
        cout << mp[num] << "\n";
      } else {
        cout << (mp.size()-mp[num]-1) << "\n";
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;
typedef long int LI;

int main() {
  int q;
  cin >> q;
  unordered_map<LI, LI> lmp;
  unordered_map<LI, LI> rmp;
  int index = 1;
  while(q--) {
    char t;
    int num;
    cin >> t >> num;
    if(t == 'L') {//O(1)
      lmp[num] = index;
      index++;
    } else if (t=='R') { //O(1)
      rmp[num] = rmp.size();
    } else if (t=='?') { //O(1)
      int size = rmp.size() > 0 ? rmp.size()-1 : 0;
      if(lmp.find(num) == lmp.end()) {
      	// found in right
      	if(rmp[num]+index-1 < size-rmp[num]) {
        	cout << rmp[num]+index-1 << "\n";
	    } else {
	        cout << (size-rmp[num]) << "\n";
	    }
      } else {
      	// found in left
      	if(index-1-lmp[num] < size+lmp[num]) {
        	cout << (index-1-lmp[num]) << "\n";
	    } else {
	        cout << (size+lmp[num]) << "\n";
	    }
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;
typedef long int LI;

int main() {
  LL q;
  cin >> q;
  unordered_map<LL, LL> lmp;
  unordered_map<LL, LL> rmp;
  int index = 1;
  while(q--) {
    char t;
    LL num;
    cin >> t >> num;
    if(t == 'L') {//O(1)
      lmp[num] = index;
      index++;
    } else if (t=='R') { //O(1)
      rmp[num] = rmp.size();
    } else if (t=='?') { //O(1)
      int size = rmp.size() > 0 ? rmp.size()-1 : 0;
      if(lmp.find(num) == lmp.end()) {
      	// found in right
      	if(rmp[num]+index-1 < size-rmp[num]) {
        	cout << rmp[num]+index-1 << "\n";
	    } else {
	        cout << (size-rmp[num]) << "\n";
	    }
      } else {
      	// found in left
      	if(index-1-lmp[num] < size+lmp[num]) {
        	cout << (index-1-lmp[num]) << "\n";
	    } else {
	        cout << (size+lmp[num]) << "\n";
	    }
      }
    }
  }
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

typedef long long int LL;
typedef long int LI;

int main() {
  LI q;
  cin >> q;
  unordered_map<LI, LI> mp;
  unordered_map<LI, LI> :: iterator it;
  while(q--) {
    char t;
    LI num;
    cin >> t >> num;
    if(t == 'L') {
      mp[num] = 0;
      for(it=next(mp.begin()); it!=mp.end(); it++) { //O(2*1e5)) --> when all queries are 'L'
        mp[it->first]++;
      }
    } else if (t=='R') { //O(1)
      mp[num] = mp.size();
    } else if (t=='?') { //O(1)
      if(mp[num] < mp.size()-mp[num]-1) {
        cout << mp[num] << "\n";
      } else {
        cout << (mp.size()-mp[num]-1) << "\n";
      }
    }
  }
  return 0;
}