$ig_nums = [4, 6]
def ig_num_keta(num)
  keta = Math.log10(num).to_i
  while num > 0
    sub_num = 10 ** keta
    if $ig_nums.include?(num / sub_num)
      return keta
    else
      num = num % sub_num
      keta -= 1
    end
  end
end

count = gets.chomp.to_i
count.times do |n|
  num = gets.chomp.to_i
  new_num = 0
  num.times do |i|
    keta = ig_num_keta(i)
    if keta.nil?
      new_num += 1
    else
      new_num += 10 ** keta
  end
end