TLE in Codeforces Beautiful graph

Sir i coded it at
https://ide.codingblocks.com/s/318054

But a TLE is occuring . Can You please Help me optimise it.