First off, please bear in mind that this is a very rare scenario for a very illiquid stock in an illiquid market. Let's say the lowest ask price for the stock is 10.00 atm and there's no bid order whatsoever on the other side; a market sell order comes in soon after sitting in the queue as there's yet any incoming/matching buy order at the moment. 30 seconds later a buy order finally comes in with a bid of 10.05 on the same exchange.
Here comes the question: does the 10.05 buy order get matched with the market order at 10.05 or does it match with the limit sell order at 10.00. My understanding is that market order generally has higher priority than limit order so it would get matched first albeit higher price. However, if price/time priority were to be valued, then intuitively it would make more sense that the lower-priced 10.00 limit sell order be matched with the buy order! This is a very rare case but can someone please shed some light on this? Thanks.