Why does white sacrifice their queen in this exercise? I was doing this exercise on lichess, but I don't see why the best move for white is to sacrifice their queen in exchange for a knight (after Nh3+):

 [title "lichess puzzle 61517"]
 [fen "4r1k1/p1b2pp1/1pp2r2/3p2np/1P1P2q1/P3P1P1/2R1NPQP/2R2NK1 b - - 1 28"]
Because white will lose the queen anyway after Nh3+, since otherwise Kh1 is answered with Rxf2 trapping and winning the queen. Just try to work out the tactics by yourself. 

As to why white doesn't give up the queen for the rook instead (after Rxf2), it's because in this position the knight+queen are more apt at coordinating and creating threats. For example, to see that even after Qxf2 black can win back the rook if desired, here's a line:

 [title "lichess puzzle 61517"]
 [fen "4r1k1/p1b2pp1/1pp2r2/3p2np/1P1P2q1/P3P1P1/2R1NPQP/2R2NK1 b - - 1 28"]

 1...Nh3+ 2.Kh1 Rxf2 3.Qxf2 Nxf2+ 4.Kg2 Nd3 5.Ra1 Qe4+ 6.Kg1 Ne1 7.Rxe1 Qxc2


One must bear in mind that these are tactics problems, and in this case we know white is lost anyhow after Nh3+ but you should solve them with the mindset that you're playing against an engine, that is, assume the best resistance that exists, and sometimes since the position is lost, the best resistance is to delay the unavoidable exactly similar to how engines would go about such lost positions. Therefore, Qxh3 is presumably the move that yields the best resistance (lowest evaluation value for black's advantage from the engine's pov).