Chef Loves The Trio : some help please my code gives wrong answers in submit idk why

@ad=[]
hash=Hash.new(0)
def f(x,i,n)
if i <= @ad.min
if x==n
return i
@ad << i
elsif x - 1<= 0
return f(3x,i+1,n)
elsif x > n
return f(x-1,i+1,n)
else
return [f(x-1,i+1,n),f(3
x,i+1,n)].min
end
else
return 1000000000
end
end
t=gets.to_i
t.times{
d,n=gets.split.map(&:to_i)
h = (Math.log((n.to_f/d.to_f).to_f,3.0).to_f).ceil
k=h + (3**h * d) - n
@ad.push(k)
puts f(d,0,n)

}

Test case of

1
11 5

should give 6, not 1000000000

//