This question is really two questions masquerading as one.
First you're asked how many caps would be produced if the price were P5? As long as P > AVC, which it clearly is at P5, the profit maximizing firm produces where P = MC. In this instance, this means producing an output of Q5... simple enough.
The question then goes on to ask, how much will be produced in
long-run equilibrium? To answer this we need to recall what long
run equilibrium is for perfect competition. We know that in long
run equilibrium firms earn zero profit, which means P = ATC = MC (which is where ATC is at the minimum). This means that in long run equilibrium the
price will be P4, so output will have to be Q4.