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

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

ans = 0

stack = []

query.each do |d, c|
  if c.even?
    s, cnt = f(d, c)

    stack << s
    ans += cnt
  else
    s, cnt = f(d, 2 * (c / 2))

    stack << d.to_s
    stack << s
    ans += cnt
  end
end

stack.compact!

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

  (a + b).to_s.chars.each do |d|
    stack << d
  end

  ans += 1
end

puts ans
