While loop will run for the maximum number of times when the Queue elements are sorted in descending order.
Let's suppose that initially, Queue elements are $16, 15, 14, 13,\ldots,2, 1$
Now, $16$ will be first pushed into the stack, So, now stack top is $16$ and $Head(Q)$ is $15$, So $16$ will be popped out of the stack ( since, "if S is Empty OR $Top(S)\leq Head (Q)$ "returns false, hence else part will be executed) and enqueued into the Queue.
So, after two iterations Queue elements will be $\rightarrow15, 14, 13, \ldots,2, 1, 16$ and stack will be empty.
Similarly, each of the elements $15,14,13,\ldots,2$ will be pushed into the stack and immediately popped out of the stack(when popped out of the stack then also enqueue into the queue).So after $30$ iterations stack will be empty and Queue contains will be like $\Rightarrow 1, 16, 15, 14, \ldots,2$.
Now $1$ will be Dequeued and pushed into the stack. Once $1$ is pushed into the stack, it will never be popped(or we can say never be enqueued into the Queue again) because in order to Pop $1$, there should be an element into the Queue which is less than $1$ and that element comes at the front of the queue, since there is no element currently present in the Queue which is less than $1$, there is no way to pop $1$.
So, after $31$ iterations Queue is $\Rightarrow 16, 15, 14, \ldots,2$ and stack contains $1$.
Now, the problem boils down to Queue with $15$ elements.
Using the similar logic we can say after another $29$ iterations (Total $=31+29$ )Queue will be like $\Rightarrow 16, 15, 14, \ldots,3$ and stack contains $1,2$ (stack top is $2$) and then $2$ will remain there in the stack forever.
Similar way if we go on then, after $31 + 29 + 27 + 25 + \ldots+1$ iterations Queue will be empty.
This is in A.P. series with $d=2$. Sum $= (16 *(1+31))/2= 16*32/2= 256$