Maths question from codeforces (short statement)

For the given positive integer n find such smallest m (n≤m) that m is a good number (The positive integer is called good if it can be represented as a sum of distinct powers of 3 (i.e. no duplicates of powers of 3 are allowed)) 1<=n<=10^18l can anyone help me with this question plzzz??

question link

what would you do for 2?
do the same for 3


thanks i got it