I am not sure how you are left with Rs 31 L after repaying the loan your savings were only Rs 10 L. I am also not sure whether the flat you are selling is a residential flat or not.
Without getting into specifics, I can only say that you need to explore the exemption u/s 54 and 54F.
If you are eligible to claim exemption u/s 54, the amount of capital gain exceeding the cost of the new house only will be shall be the capital gains chargeable. Thus in the present case, if the amount of capital gain is less than or equal to Rs. 31 L, his investment of Rs 31 L will qualify for exemption.
If you are eligible to claim exemption u/s 54F, the amount that should be invested is the net consideration received. If the amount is less than the net consideration, the exemption will be proportionate to the amount invested.
The repayment of existing housing loan will not qualify for exemption u/s 54F.