Problem:
Score : 400 points

Problem Statement

You start with the number 0 and you want to reach the number N.

You can change the number, paying a certain amount of coins, with the following operations:

Multiply the number by 2, paying A coins.

Multiply the number by 3, paying B coins.

Multiply the number by 5, paying C coins.

Increase or decrease the number by 1, paying D coins.

You can perform these operations in arbitrary order and an arbitrary number of times.

What is the minimum number of coins you need to reach N?

You have to solve T testcases.

Constraints

1 \le T \le 10

1 \le N \le 10^{18}

1 \le A, B, C, D \le 10^9

All numbers N, A, B, C, D are integers.

Input

The input is given from Standard Input. The first line of the input is

T

Then, T lines follow describing the T testcases.
Each of the T lines has the format

N A B C D

Output

For each testcase, print the answer on Standard Output followed by a newline.

Sample Input 1

5
11 1 2 4 8
11 1 2 2 8
32 10 8 5 4
29384293847243 454353412 332423423 934923490 1
900000000000000000 332423423 454353412 934923490 987654321

Sample Output 1

20
19
26
3821859835
23441258666

For the first testcase, a sequence of moves that achieves the minimum cost of 20 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 1 to multiply by 2 (x = 4).

Pay 2 to multiply by 3 (x = 12).

Pay 8 to decrease by 1 (x = 11).

For the second testcase, a sequence of moves that achieves the minimum cost of 19 is:

Initially x = 0.

Pay 8 to increase by 1 (x = 1).

Pay 1 to multiply by 2 (x = 2).

Pay 2 to multiply by 5 (x = 10).

Pay 8 to increase by 1 (x = 11).

Solution in Rust (1.42.0) (judge verdict: Wrong Answer)

#![allow(non_snake_case)]

use std::cmp::min;
use std::io::{stdin, Read};
use std::str::FromStr;
fn read_option<T: FromStr>() -> Option<T> {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok()
}
fn read<T: FromStr>() -> T {
    let opt = read_option();
    opt.expect("failed to parse token")
}

fn main() {
    let T: usize = read();
    for _ in 0..T {
        let (N, A, B, C, D): (i64, i64, i64, i64, i64) = (read(), read(), read(), read(), read());
        let ans = solve(N, A, B, C, D);
        println!("{}", ans);
    }
}

#[allow(dead_code)]
fn solve(N: i64, A: i64, B: i64, C: i64, D: i64) -> i64 {
    use std::cmp::Reverse;
    use std::collections::{BinaryHeap, HashMap, VecDeque};
    let mut dp = HashMap::new();
    let mut new = BinaryHeap::new();
    dp.insert(N, 0);
    new.push((Reverse(0), N));
    let mut min_cost: i64 = match N.checked_mul(D) {
        None => std::i64::MAX,
        Some(v) => v,
    };
    while let Some((Reverse(now_cost), v)) = new.pop() {
        if v == 1 {
            min_cost = min(min_cost, now_cost);
            break;
        }
        if now_cost >= min_cost {
            continue;
        }
        for &(d, cost) in &[(2, A), (3, B), (5, C)] {
            let nexts = match v % d {
                0 => vec![(v / d, 0)],
                rem => vec![
                    ((v - rem) / d, rem * D),
                    ((v + (d - rem)) / d, (d - rem) * D),
                ],
            };
            for (next, add_cost) in nexts {
                // 後者は全部1を引いた場合と比較している。全部1が少ないコストの場合はadd_costなしで最初からv - nextに直線で行けば良い
                let new_cost = match (v - next).checked_mul(D) {
                    None => now_cost + cost + add_cost,
                    Some(v) => min(now_cost + cost + add_cost, now_cost + v),
                };

                // if next == 3 {
                //     let rem = v % d;
                //     dbg!(v, now_cost, new_cost, add_cost, cost, A, B, C, D);
                //     dbg!(
                //         rem,
                //         v,
                //         d,
                //         v - rem,
                //         v + (d - rem),
                //         ((v - rem) / d, rem * D),
                //         ((v + (d - rem)) / d, (d - rem) * D),
                //     );
                // }
                match dp.get(&next) {
                    None => {
                        dp.insert(next, new_cost);
                        new.push((Reverse(new_cost), next))
                    }
                    Some(&cost2) => {
                        // 新しい道のほうが安い場合はコスト更新するためにキューに突っ込む
                        if cost2 > new_cost {
                            dp.insert(next, new_cost);
                            new.push((Reverse(new_cost), next))
                        }
                    }
                }
            }
        }
    }
    min_cost + D
}

#[cfg(test)]
#[allow(unused_imports)]
mod tests {
    use super::*;

    #[test]
    fn test1() {
        assert_eq!(solve(11, 1, 2, 4, 8), 20);
    }
    #[test]
    fn test2() {
        assert_eq!(solve(11, 1, 2, 2, 8), 19);
    }
}