If we have an integer(say ) which ends in either 1,3,7 or 9 then how can one find another integer having at most the same number of digits as but when cubed would end in exactly the same digit as .

E.g if a = 123

then the required number is 927 because 123 ends in 3 and 927^3 also ends in 3 .

Other examples:

a = 435621 then required number is 786941

Thanks.