M = gets.to_i

query = M.times.map { d, c = gets.split.map(&:to_i) }

def f(d, c, cnt = 0, str = '')
  return [d.to_s + str, cnt] if c == 1
  return [nil, 0] if c == 0

  str = d.to_s + str if c.odd?

  case d
  when 0
    return ['0', c - 1]
  when 1
    return f(2, c / 2, cnt + c / 2, str)
  when 2
    return f(4, c / 2, cnt + c / 2, str)
  when 3
    return f(6, c / 2, cnt + c / 2, str)
  when 4
    return f(8, c / 2, cnt + c / 2, str)
  when 5
    return f(1, c / 2, cnt + c, str)
  when 6
    return f(3, c / 2, cnt + c, str)
  when 7
    return f(5, c / 2, cnt + c, str)
  when 8
    return f(7, c / 2, cnt + c, str)
  when 9
    return f(9, c / 2, cnt + c, str)
  end
end

ans = 0

stack = []

query.each_with_index do |(d, c), i|
  s, cnt = f(d, c)

  stack << s
  ans += cnt
end

stack = stack.compact.map { |s| s.chars }.flatten

while stack.size > 1
  a, b = stack.pop(2).map(&:to_i)
  sum = a + b

  if sum >= 10
    ans += 1
    stack << (sum % 10 + 1).to_s
  else
    stack << sum.to_s
  end

  ans += 1
end

puts ans
