#include <bits/stdc++.h>

using namespace std;
const int N = 2e5 + 50;
int pos[N], l, r;

int main() {
    int Q, id;
    char op[3];
    scanf("%d", &Q);
    for (int q = 0; q < Q; q++) {
        scanf("%s%d", op, &id);
        if (q == 0)
            pos[id] = 0;
        else
            switch (*op) {
                case 'L':
                    pos[id] = --l;
                    break;
                case 'R':
                    pos[id] = ++r;
                    break;
                case '?':
                    printf("%d\n", min(abs(pos[id] - l), abs(pos[id] - r)));
                    break;
            }
    }
    return 0;
}