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.

Example

Input

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


Output

20
19
26
3821859835
23441258666

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
    public static void main(final String[] args) {
        final FastScanner scanner = new FastScanner(System.in);
        final int t = scanner.nextInt();
        for (int i = 0; i < t; i++) {
            final long n = scanner.nextLong();
            final long a = scanner.nextInt();
            final long b = scanner.nextInt();
            final long c = scanner.nextInt();
            final long d = scanner.nextInt();
            final BigInteger bigD = BigInteger.valueOf(d);

            final Map<Long, Long> memo = new HashMap<>();
            memo.put(n, 0L);
            final Deque<Long> queue = new ArrayDeque<>();
            queue.add(n);
            while (!queue.isEmpty()) {
                final long key = queue.poll();
                if (key == 1) {
                    continue;
                }
                final long value = memo.get(key);

                if (key % 2 == 0) {
                    final long min = BigInteger.valueOf(a).min(BigInteger.valueOf(key / 2).multiply(bigD)).longValue();
                    if (!memo.containsKey(key / 2) || memo.get(key / 2) > value + min) {
                        memo.put(key / 2, value + min);
                        queue.add(key / 2);
                    }
                } else {
                    final long inc = key + 1;
                    long min = BigInteger.valueOf(a).min(BigInteger.valueOf(inc / 2).multiply(bigD)).longValue();
                    if (!memo.containsKey(inc / 2) || memo.get(inc / 2) > value + d + min) {
                        memo.put(inc / 2, value + d + min);
                        queue.add(inc / 2);
                    }

                    final long dec = key - 1;
                    min = BigInteger.valueOf(a).min(BigInteger.valueOf(dec / 2).multiply(bigD)).longValue();
                    if (dec != 0 && (!memo.containsKey(dec / 2) || memo.get(dec / 2) > value + d + min)) {
                        memo.put(dec / 2, value + d + min);
                        queue.add(dec / 2);
                    }
                }

                if (key % 3 == 0) {
                    final long min = BigInteger.valueOf(b).min(BigInteger.valueOf(key / 3 * 2).multiply(bigD)).longValue();
                    if (!memo.containsKey(key / 3) || memo.get(key / 3) > value + min) {
                        memo.put(key / 3, value + min);
                        queue.add(key / 3);
                    }
                } else {
                    final long mod = key % 3;
                    final long dec = key - mod;
                    long min = BigInteger.valueOf(b).min(BigInteger.valueOf(dec / 3 * 2).multiply(bigD)).longValue();
                    if (dec != 0 && (!memo.containsKey(dec / 3) || memo.get(dec / 3) > value + mod * d + min)) {
                        memo.put(dec / 3, value + mod * d + min);
                        queue.add(dec / 3);
                    }

                    final long reverseMod = 3 - mod;
                    final long inc = key + reverseMod;
                    min = BigInteger.valueOf(b).min(BigInteger.valueOf(inc / 3 * 2).multiply(bigD)).longValue();
                    if (!memo.containsKey(inc / 3) || memo.get(inc / 3) > value + reverseMod * d + min) {
                        memo.put(inc / 3, value + reverseMod * d + min);
                        queue.add(inc / 3);
                    }
                }

                if (key % 5 == 0) {
                    final long min = BigInteger.valueOf(c).min(BigInteger.valueOf(key / 5 * 4).multiply(bigD)).longValue();
                    if (!memo.containsKey(key / 5) || memo.get(key / 5) > value + min) {
                        memo.put(key / 5, value + min);
                        queue.add(key / 5);
                    }
                } else {
                    final long mod = key % 5;
                    final long dec = key - mod;
                    final long min1 = BigInteger.valueOf(c).min(BigInteger.valueOf(dec / 5 * 4).multiply(bigD)).longValue();
                    if (dec != 0 && (!memo.containsKey(dec / 5) || memo.get(dec / 5) > value + mod * d + min1)) {
                        memo.put(dec / 5, value + mod * d + min1);
                        queue.add(dec / 5);
                    }

                    final long reverseMod = 5 - mod;
                    final long inc = key + reverseMod;
                    final long min2 = BigInteger.valueOf(c).min(BigInteger.valueOf(inc / 5 * 4).multiply(bigD)).longValue();
                    if (!memo.containsKey(inc / 5) || memo.get(inc / 5) > value + reverseMod * d + min2) {
                        memo.put(inc / 5, value + reverseMod * d + min2);
                        queue.add(inc / 5);
                    }
                }
            }

            System.out.println(memo.get(1L) + d);
        }
    }

    private static class FastScanner {
        private final BufferedReader reader;
        private StringTokenizer tokenizer;

        FastScanner(final InputStream in) {
            reader = new BufferedReader(new InputStreamReader(in));
        }

        String next() {
            if (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (final IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }
    }
}
    