M = gets.to_i

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

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

  case d
  when 0
    return ['0', c - 1]
  when 1
    return f(2, c / 2, cnt + c / 2)
  when 2
    return f(4, c / 2, cnt + c / 2)
  when 3
    return f(6, c / 2, cnt + c / 2)
  when 4
    return f(8, c / 2, cnt + c / 2)
  when 5
    return f(1, c / 2, cnt + c)
  when 6
    return f(3, c / 2, cnt + c)
  when 7
    return f(5, c / 2, cnt + c)
  when 8
    return f(7, c / 2, cnt + c)
  when 9
    return f(9, c / 2, cnt + c)
  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
