fromsortedcontainersimportSortedListclassMRUQueue:def__init__(self,n:int):# [(priority value, actual value)]self.q=SortedList((i,i)foriinrange(1,n+1))deffetch(self,k:int)->int:_,num=self.q.pop(k-1)ifself.q:maxPriority=self.q[-1][0]self.q.add((maxPriority+1,num))else:self.q.add((0,num))returnnum