#include <bits/stdc++.h>
using namespace std;
/*
Coded by 秦惜梦
The most attractive girl in the world
.........   `;!!!!!!!!!;;;;;;;;::::::'''''''``''''''''````````````````````....`:;;;;;:'.....`:|$$$$$$$|;;;:`.    ..........`''``...`''``.   ........   ...  ...   ....  ...   ...  '
........     '!!!!!!!!!;;;;;;;:::::::::''''':::''''`````````````````````......`:;;;;;!%$$%!'.....`:|$%!;;;'....   .....`....`'''`...```..    ........  ..   ...    ..   ...  ...   '
........     .:!!!!!!!!;;;;;;;:::::::::::::::'''''''`````````..```````........`:;!%$$$$$$$$$$$%!'.....`:|!'````..   .........`''``.......    ........  ...  ...   ...   ..   ..   .'
.......       `;!!!!!!;;;;;;;;::::::::::::::::'''''```````````.``````...       .`';|%$$$$$&&$$$$$$%%;'..`'':'''``..  ...``.....```.   ....    .......  ...  ...   ...  ...  ...   .'
.......      . '!!!!!!;;;;;;;:::'''''''''::::::'''```````````````````.........':'`.......`:!%%|||%$$$$$&%;'::::''``.   .```..   ....   ....   .......   ..  ...   ..   ...  ...  ..'
......      '::'''':::::::::::::'''''''''''::::''''``````````````````.........'!$$$$$$%!:`.......`:!%&&&|'``':::'''``.....```.   ....   ...    ......   .......   ..   ..  ...   ..'
``...       .`.``'::::::::;;;;:::::'''''''`'''''`````````````..```````........'!$$$$$$$$$$$$$$%!;'`.....`'`..`'::''''`.....```..  ....   ...   ......   ........ .... .......  ....'
````.      .````.`;!!!;;;;;;;;::'''''''``'''''`````````````````````````.. ....`:!|%$$$&&&&&&&$%%$&&&&@&$|::'`.`''''''''`....````.  ....   ...   ......   .  .....```..``.....  ... '
````      .````.  `;!!;;;;;;;:::::'''''':::'''''````````````........`````.... ........````........``'::::::::'``.``'''''``...````.. ....   ...  ......   .  ......``.```....   ..  '
```.      .```.  .`';!!;;;;;;;:::::::::::::'''''``````````..........````````...`;$$$$&&&&&&$%%||||!|!;;::::::::'`...`'''''`....````.  .... ...   .....   .   .. .``..```....  ..   '
``.       .``.  .`'`';!;;;;;;;;:::::::::::'''''`````````...........`````````....'!|%%%%%%%%%%%%%%%%%%$&&&|:'''''''`...`'''''`....```.  .... ...   .....  .   ..  ..  ......   .   .'
`.        .`.  `''''`';!;;;;;;;::::::::::'''''''```````..........````````````....:|%%%%%%%%%%%%%%%%%%%%%%|:`''''''''`...`''''`....```.. ...   ..  .....  ..  .   ..  .. ...  ..  ..'
`         .  .`'''''``';!;;;;;;;:::::::''''''''''```.``........```````````````````!%%%%%%%%%%||!;:''`.........`'''''''`...`''''`...````.....  ..   ....  ..  .   .   .  ..  ..  ...'
            .''''''````';!;;;;;;;:::::'''''''''''```````````````````````````.........``````...  ...```':;!!:....`'''''''``..`'''`....```.  ... ..  ....      ..  .   .  ..  ..  ...'
         .`''''''''``````;!;;;;;;:::::'''''''''''``````````'''''''''``....```````````````````````````':!||%|;``....`''''''``..`''``....`..  ...     ...   .  .   .  .. ..  .   ... '
        .'''''''''````````:;;;;:::'''''':::::::::::::''''''''''''``..``````....```````````````````````````:!!'`'`.....`'''''``...```.  .........    ....     .  ..  .  .  ..  ...  '
        `'''''''''````';;;:::::::::::::::::::::::''''```````````..........```````````````````````````````````'`.``'``....`''''``.. .`.. ..... ...    ...        .  .. ..  .   ..   '
       .'''''''''```````';;:::::''''''''''''''''''`''```````.`````.....````````````````````````````````''``````....``''`...``''```......  ........   ...     .     .  .  .   ..    '
       `''''''''````'''`````.`::::::::'''''''''''''''''``.``.```........````````````''''`````````````````''''''''`....``'``...``````...... ........   ..     .  .  . .   .  ..     '
      .`'''''''```'''''````````':::::::''''''''''''''``.``.`............`````````'''''''''''''''``````````'''''''''`.....````....```...  ..  ......    .                .   .     .'
      `'''''''`''''''''````````.`::::::'''''''''''''`.```..............``````````'''''''''''''''''````````''''''''''``.......```.........  .  ....     .     ........              '
     .`''''''`''''''''``````````.`:::::::'''''''''``.`..............```````'''''''''''''''''''''''''``````'''''''''''''.........``... .....  .  ...     ..`:::::::::::::'`.        '
      .'''''`''''''''``````````````':::::''''''''``..````````````````````''''''''''''''''''':''''''```''````''''::''''''`.```..........  .....   ....    `'::::::::::::::::`.      '
     ....`''''''''''```````````````.`':::::'''''`...``````````''''''''''''''''''''''''''''''';:'''``''`::````';:''''''''''.....``......... .....   ...   `::::::::::::::::::'`.....'
    ......`''''''''``````````````````.':::::::'`...``'''````''''''''''''''''''''''''''''''';!:;:'''':!;::'```````''''''''''`   ........ ..... ...    ..  ....`':::':::::::::::'... '
    .......`'''''`````````````````````.`':::'```.`''''''````''''''''':'''::'''::''':;::!!!;:;!;;:```````::`````````'''''''''`.     ............  ..   ..          .':::::::::''`   '
   ........`''`````````````````````````..`':''`:;:'':''':''''''''''''::'':;:'''''''''::'''''':;;;:``````:;'```''``````'``'''''`.... ...........    ..                 `::::::'`.   '
  ..........`````````````````````````'::::''':;!!!!;:'::':!;;;:::'''';;'''::::''''':'''::'''''''::'`````':''``''````::`':'''''''''.         ......                      .''`....   '
.`'`.......```````````````````````````````'':!;:::::::::':;::::::::''::''`''::::'`''''':::'':''`:;'`````':'```''`````::`````'''''':;'..................                 .......  . '
`'''''```````````````````````````````````.`;!:::::::::::'''::::::::''::''``'::::'``'''''::''''''':'````````````'`````''````````````:!!;'................              ..........   '
'''''`````````````````````````````````````;!;;;:':::''::''''::::;;:'':::'```'::'````''''''''''''':'```````````''````````````````````';!!!;'....  ......               .......      '
''''````````````````````````````````````';!;;;:'::::':''`''':::;;;:'''':'``'':'````.```''''''''`':'````````````'`````````..``....`...`:!!!!:`.`.....                    .          '
'''````````````````````````````````````':;!;;:`'::':'''``''':::;;;:''``'```'':'``....''`'''''''`''`.`````````````.````````.............';;'.`:;;;'`.                 .....   ...   '
''````````````````````````````````````'':!;::'`::':::''`''''':;;;;:```````'''''```...''`'''''''`''`.``.````````````````````............... `;;;;;;;;;:`.                ....   ....'
''''''''````````````````````````````''`:!;;:''':''::''`'':''':;;;;'```'```''''''`.`'``:'''::''''''```````````````````````````.............`:;;;;;;;;;;;'.      .         ....    ..'
:::::::''''````````````````````````''`'!!;:''''''':''`''''''':;;;:```::``'''':'''`';:';:'::::':`''`''`````````````````'``````````..........`:;;;;;;;;;:.     `'..          .....   '
|%$$%%!;;:'''````````````````````''``';!;:'''':'``'''`'':''':::;:'``:!:``':::::::`:!;';;':::::''`''::````'''''````````''`````````...........`:;;;;;;;'.    .':` `;;;;:. ..   ..... '
@@@@@@@@$|;:''`````````````````''```';!;:::::::'``````'':''::::::'`'!|:`':::;:::'';|!:;;'''''''::::;:``'::''`'````````''````````````.........`:;;;;;`     .:;'..';;;;;;;;;:` ......'
@@@@@@@@@@$|;:''`````````````:'`````;;;:';;;;::'`'````'':''::::::'`;!!;':::::::'`.';!;;;:::::';!!::;'`':::'``'`.``````''````````.......... ..``';;:`     .';;` `:;;;;;;;;;:` `;;;;;;
@@@@@@@@@@@&|;:'``````````''```````;;::':;;;;:'`''```'':'::::;;;:':;|!!;:'''''::::!||!!!!;!!!!||;:!'`:::::'````..``````''`````.......... .  ..`':;`     .';;:` ';;;;;;;;;;:. ';;;;;;
@@@@@@@@@@#@$!:''`````````````````::```';;;;::``'''``'::':::;;;::;;!|||!::;;!!!;;;:'``````'':!!!!|:``':::'`````:;:`.``````.............   . ..`';'     .';;;'..:;;;;;;;;;;'..';;;;;;
@@@@@@@@@@@@$|;:'````````````````::````:;;;::'.`''```''''':::;:::;||%|||%%%%%|!:''''`...`'':!;:;|;```:::'``''`';!;'..`````..............  . ..`''.    .';;;:` `;;;;;;;;;;;' .:;;;;;;
@@@@@@@@@@@&%!;:'```````````````''````':;;;::'..''```````:::::';||%%%%%%$$&&&%!;!|$&%:..';!%%!!|!'`;::::'``''';!;;'...````..............  ....``..   .';;;;'..';;;;;;;;;;;` `:;;;;;;
@@@@@@@@@#@%!!;:'````````````````````.'''::::`..````'''::!||||%%%%&&&&&&&&&&&&&$&&&%||||!|%%%%|!:';;;;!;``''`:;;;:`....`...................`....```. .`':::` `:;;;;;;;;;;:` `;;;;;;;
@@@@@@##@$|!!;:''```````````````````````':'```..`````':!;:''''':!%$&&&&&&&&&&&&$$$|!;;!|%%%%%%|;':;''::'`'''`:!;:`........................```..````........  .':;;;;;;;;;'. ';;;;;;;
!%$$$$%%|!!!;:''``````````````````````````````````````';!:'::'``':|$&&&&&&&&&&&&$$$$$$$$%%%%%|!:```''::'''''```...........................``..`'''`........   ......'::;;'..';;;;;;;
;!!!!!!!!!!;:''``````````````````````````````..````````'!%!;!;;!!%$%$&&&&&&&&&&&&$$$$$$$$$$%%||;``'''':''''``..`....    ... .................`'''````.....   .....`.......  `::;;;;;
::;!!!!!;;:''````````````````````````````````...````````'!%|!|%$$$$%$&&&&&&&&&&&&&&$$$$$$$$%|||:``''''''''```..`....      ..    ...     ....`'''`''```....   .'|%%;......    .... .'
``'::::'''````````````````````````````````````..`...```'`'|&$$$$$$$%$&&&&&&&&&&&&&&$$$$$$$$%%%|:`'''''''''```..`....                    ....`''```''```... .:%%%%|:......   .......'
 .````````````````````````````````````````````.``..```````:|&$$$$$$|%$&&&&&&&&&&&&&&&&&$$$$%%%|:'`'''''''````..`....                      .``'`````''````. :%%%%%;`....... ........'
  .``````````````````````````````````````````..`..`````````:%&&&&&&$%|%$&&&&&&&&&&&&&&&&$$$%$%|:'`'''''''````.``.....                    .``'`````'`'':|:.'!%%%%|'.`````.   .......'
  .```````````````````````````````````````````'`..`````````.:$&&&&&&&&&&&&&&&&&&&&&&&&&&&$$$$%|;'```''''```````.......                  .``'```'``':|%%|'.'|%%%%;```````...........'
  .`````````````````````````````````````````:'`..''````````..!&&&&&&&&&&&&&&&&&&&&&&&&&&&$$&$%%;''```''````````....   .                .'''`.`'``'`;%%%;``;%%$%!'..................'
  .``````````````````````````````````````;|:.``.`''''`````...`|&&&&&&&&&&&&&&&&&&&&&&&&&&$$&&$%!:'```````````.``..                    .`''```;;'''';%%%:.:%&&$|:...................:
  .```````````````````````````````````'!!```'`.`:!:''`````. ..`|&&&&&&&&&&&$%%$$$%|%$$&&&$$&&&$%;'`.``..`......`.                   ..`''''!;''''':|%$|'`!&&$|:...................`:
   .````````````````````````````````;|:````'::`'!;::''''`.  ...`!&&&&&&&&&&&&&&$&&&&&&&&&&$&&&&$|:'```.``.........        .`.    ....`'''```''''';$&&&;`:!;`......................`:
   .`````````````````````````````:|;`````'';:'`';:'''```.. ......'|&&&&&&&&&&&%%%$&&&&&&&$$&&&&&%;'`'`..```........         ``  `;:`''''``'```'!$&&&%!'.....................```````:
   .``````````````````````````:|;``````'':;:'``':'''`````. ........`:%&&&&&&&&&&&&&&&&&&&$&&&&|!!!;'''`....``......`.        .;%%;`''''';|||!;:''::;;' ..``......``````````````````:
   .```````````````````````!%:````````'':;:'```'':''````.. ............'|&@&&&&&&&&&&&&&&&$$|;!!!!!;:'``'`....``...````..      `:;;:'''!%;::::::::;;`. .````````'''````````````````:
   .```````````````````:%!'``````````'::::'````'''''````.  .............```;$&@&&&&&&&&&&$|;;!!!!!!!!;```::`  ...``.';;'```.    `:::'';$$|;:::::!;'`....``````''''''''````''''`````:
   .```````````````:%|'`````````````'::::'`````'''''''``.  .....  ....`````````;$&&&&@&|::!!;!!!!!!!!|!'```';'..  ....'!%%|!::``'':'':|$&&$|!!;:;:'`. .```````':::::''''''''''''''':
   .``````````;||:```````````````''::'::'````.`'''''''`.. .......  ..`````````````';:`````';!!!!!!!||||!;'```';!:`... ..`:|%%!'''''`:!%$&&&$$$!:;;;:'``'```''::::::::''''':::::'''';
   ..``';;:'```````````````````'''::'':'```````'''::''`..  .......   ..``````````````````. '||!!!|||||||!!;'````'!|;'``````````'''`';:'`'%&&&&&&|;!;:;!!!;;::;;::::::''''::::::::::;
``...``````````````````````''''''::''''`````````':::''``. .........  ....```````````...   .:!||||||%%%|||!!!!;'````'!||;'```.`'''`.`.`''`.;$&&&$:  .``'::::;!|%$$%|;:''':::::::::::;
    .````````````````````'''''''::```'````````.`:::'''```. .......... .............    `!%|!!||||%%||||!!!!!|%%%!:````````..`'''..``''`.....':. .   ..`':;;;;;;;::::;!%&%!;;;;:::::;
    .`````````````````''''''''''''''::'''````..`::''''`````. ........``............ .;%%%|!!!||%%%%%%%|!!!|%%%%%%||%%%%%%!'`'''`````....``.....       ..  .`':;;;;;;:::::;!%%!;;;;;;
    .`````````````'''''''''''```''''::'''''``..`''''''```````........````````.....:%%%$%||!|||%%%%%%%||!|%%%%%%%%%%%%|||!'`'''';%%|;`````......'`.      ..      `':::::::;;;;;|%!;;;
    .``````````''''''''''```````'```''''````..`.`'''''````````....  ..```....  `|%%%%$%|||!|%%%%%%%%%|!|%%%%%%%%%%%%%%%|:`'''`;%%%%!:.:!|%$$$$|;:'`...   .``..  .`.  '::::;;;;;;!%!;
%!'..```````''''''''''``````````'```'````````.``.`'''''```````.......       .;%%%%%%%|!!!||%%%%%|!!!!!|%%%%%%%%%%%%%%%%:`''''!%%%!!!:`:!|%$$$$$$$$$%|'`.  .```'`.``.  ..     .':::|%
%%%%%%;```''''''''```````````````'``''``````.````.`''''`..'````.......`'''';|%%%%%%%%||%%%%%%%%%%%|!!%%%%%%%%%%%%%%%%%:`'''';%%||||:`:!|%$$$$$$$$$$$$$%;``. .``''`.                '
$$$%%%%%$$|:''````````````````````''''``````.`````.`'''''`...      .`':|%$$%%%%%%%%%|!|%%%%%%%%%%|||%%$$%%%%%%%%%%%%%:`'''';%||%%%;.:||%$$$$$$$$$$$$$$&&&%:...````''`              '
$$$$$$$$$$%%%%%;`.``````````````````':'``````.`````.`'''''`......`''':!%%%%%%%%%%%%|!!%%%%|||%%|!|%%%%%%%%%%%%%%%%%%:`'''';||%%%|:`;||%$$$$$$$$$$$$&&&&&&&$|'...```'''`            '
|%%$$%%$$$$$%%%%%%%;`..`.`````````````':'````.``.```..`''````''''':|!:|%%%%%%%%%%%%|!!|%|||%||!!%%%%%%%%%%%%%%%%%%%:`'''':|%%%%;``;|%%$$$$$$$$$$&&&&&&&&&&&$$;...`````''`          '
@$|||||%%%%%%%%%%%%%%%%|:`....```````.`''``'''`````````.````''':;|%|;:|%%%%%%%%%%||!!|||||||!!|%%%%%%%%%%%%%%%%%%%:`'''';|%%%%:.'!|%$$$$$$$$$$$$$&&&&&&&&&&&&$!`..``````''`        '
||||&@&%|!|||%%%%%%%%%%%%%%|:.....```````'````.........`!%%%%%%%%|||;:|%%%%%%%%%%||!!|%|%|!!||%%%%%%%%%%%%%%%%%%%:`'''';|%%%!'`;||%$$$$$$$&&$$$$$&&&&&&@&&&&&$$!`..``````````.     '
|||||!|!|$@&%|!|||%%%%%%%%%%%%%:. ...`````''``........:|%%%%%%%%%||%;:|%%%%%%%$%%%%!;|||!!||||%%%%%%%%%%%%%%%%%%:`'''';||%|:`:||%%$$$&&&&&$$%%$$&&&&@######&&$$$!`..``..```````.   '
||||||||!|!!!!|%$%|||||%$%%%%%%%%%!`.....````'`......'|%%%%%%%%%%|%$|;%&&&&&&&&$$$$|!|!!%$$$$&&&&&&&&&$$$$$$$$%;`''':;|||;``;||%$$&&&&&&&$$%%$$&&&&&@######&$$$$%:.....``````````. '
||%%||||||!!|!!|||!|%$%|||%%%%%%%%%%%|:.....`.`'`...'|%%%%%%%%%$$$$&|:|&&&&&&&&$$$$|;!|$$$$&&&&&&&&&&&&&&&&@&$;`''':;||;``!||%$&$$$&&&&$%%%%%$&#####@@###@&$$&$$$!`.......`````````:
   .:!|%%|||||||||||||||!|||||%%%%$%%%%%;`.......``:%&&&&&&&&&&&&$$$%;;$&&&&&&&&&&$||$$$&&@@@@@@@@@@@@@@@@@@&!'''':;!:`'!||%$&$$$$$&&&$%%|%%$@######@&&&&&$$$$$$$%;..`...`...`````':
    .....`;|%%%%%|||!|||!|||!!!||%%%%%%%%%%;`.....:%&&&&&&&&&&&&$$$&$|'!$&&&&&&&$$%$$$&&@@@@@@@@@@@@@@@@@@@@!'::':'`.'|||%$$&&$$$$$&&$%%||%$@#########@@&&$$$$&$%$!`..`...```..````:
    ..........'!|%|%|||||!||||||||||%%%%%%%%%:...'|&&&&&&&&&&&&&$$&&&$;'|&&&&&$%%$$$&&@@@@@@@@@@@@@@@@@##@@!'::::'`:|%%%$$$&&&$$$$$$$%%|%&@@##########@@@@&&&$$$$$%;..``....````.``:
    .....```......`;|%|%%||||||||||||||%%%%%%|!:`!&&&&&&&&&&&&&&$$$&&$%::%&$$%%$$&&&@@@@@@@@@@@@@@@@@@@#@&|::::::;|%%$$$$&&&&&$$$$$$%%%%&@@@###########@@@@&&&&&$$$!`..`.....`````.'
    ..................`;|%%%|%%|||!|||||||%%%|||$&&&&&&&&&&&@&&&$$$&&&$%;;%%%$$&&@@@@@@@@@@@@@@@@@##@#@&$;':::::!%%$$$$&&&&&&&$$$%%%%%|$@@@@##@#########@@@@@&&&&$$|:...`.....`````:
    ............``..```...`;|%|||%||!!|||||||||%&&&&&&&&&&@@@@&&$$$&&&&$%;;%$$&@@@@@@@@@@@@@@@@@@#@@&$!`.`:::::!%$$$&&&&&&&&$$$$$$%%%||&@@@@##@@@@########@@@@&&&&$$!`...`.....````:
    ...................```...`:|%||||||||!||||%&&&&&&&&&&&@@@@@&$%&&$$$%%%;:!$&@@@@@@@@@@@@@@#@@&&%;``;|;:::::!$$$&&&&&&&&&&$$$$$%%%%||&@@@@@@@@@@##@#####@@@@@&&&&&$:..```.....```:
    .....................``.`...`;|%|||||||||%&&&&&&&&&&&&@@@@@@$$&$$%%$$$%!::|&@@@@@####@@@&$%;'':|%|!::::::!$$&&&&&@&&&&&&$$$$$$%%|||$@@@@@@@@@@@@@@@####@@@@@&&&&&%'.```..`...``:
    ...........................``..'!%|||||!%$&&&&&&&&&&&&@@@@@&$$$%%$$@@@&$%!::|&&&&&&&&$|;::;||||%%|::::::!$$&&&&@@@&&&&&&$$$$$$%%|||$@@@@&&&&&&&@@@@@@@@@@@@@@&&&$$|'.```.......:
    ...........................```...`;||||%$&&&&&&&&&&&&@@@@@&$%%$$&@@@@&&&&$|!!;;!||;:;!|%%||%$&&$|;:::::!$$&&&@@@@@@@&&&&$$$$$$%%%%|%&@@@&&&&&&&&&&&&&&&&&@@@&&&&$$$!`.``.......:
*/

typedef long long ll;
ll n;
ll a,b,c,d;
const ll ieq=1000000000;

void solve(){
	map<ll,ll> dist;
	scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&d);
	priority_queue<pair<ll,ll>> q;
	q.emplace(0,n);
	dist[n]=0;
	while(q.size()){
		pair<ll,ll> X=q.top();
		q.pop();
		ll dis=-X.first,cur=X.second;
		if(dist[cur]!=dis){
			continue;
		}
		if(!cur){
			printf("%lld\n",dis);
			return;
		}
		if(cur%2==0){
			ll cs=a;
			if(cur/2<ieq){
				cs=min(cs,cur/2*d);
			}
			if(!dist.count(cur/2)||dist[cur/2]>dis+cs){
				dist[cur/2]=dis+cs;
				q.emplace(-(dis+cs),cur/2);
			}
		}else{
			ll cs=a+d;
			if(cur-cur/2<ieq){
				cs=min(cs,(cur-cur/2)*d);
			}
			if(!dist.count(cur/2)||dist[cur/2]>dis+cs){
				dist[cur/2]=dis+cs;
				q.emplace(-(dis+cs),cur/2);
			}
			cs=a+d;
			if(cur-cur/2-1<ieq){
				cs=min(cs,(cur-cur/2-1)*d);
			}
			if(!dist.count(cur/2+1)||dist[cur/2+1]>dis+cs){
				dist[cur/2+1]=dis+cs;
				q.emplace(-(dis+cs),cur/2+1);
			}
		}
		if(cur%3==0){
			ll cs=b;
			if(cur-cur/3<ieq){
				cs=min(cs,(cur-cur/3)*d);
			}
			if(!dist.count(cur/3)||dist[cur/3]>dis+cs){
				dist[cur/3]=dis+cs;
				q.emplace(-(dis+cs),cur/3);
			}
		}else{
			ll cs=b+d*(cur%3);
			if(cur-cur/3<ieq){
				cs=min(cs,(cur-cur/3)*d);
			}
			if(!dist.count(cur/3)||dist[cur/3]>dis+cs){
				dist[cur/3]=dis+cs;
				q.emplace(-(dis+cs),cur/3);
			}
			cs=b+d*(3-cur%3);
			if(cur-cur/3<ieq){
				cs=min(cs,(cur-cur/3-1)*d);
			}
			if(!dist.count(cur/3+1)||dist[cur/3+1]>dis+cs){
				dist[cur/3+1]=dis+cs;
				q.emplace(-(dis+cs),cur/3+1);
			}
		}
		if(cur%5==0){
			ll cs=c;
			if(cur-cur/5<ieq){
				cs=min(cs,(cur-cur/5)*d);
			}
			if(!dist.count(cur/5)||dist[cur/5]>dis+cs){
				dist[cur/5]=dis+cs;
				q.emplace(-(dis+cs),cur/5);
			}
		}else{
			ll cs=c+d*(cur%5);
			if(cur-cur/5<ieq){
				cs=min(cs,(cur-cur/5)*d);
			}
			if(!dist.count(cur/5)||dist[cur/5]>dis+cs){
				dist[cur/5]=dis+cs;
				q.emplace(-(dis+cs),cur/5);
			}
			cs=c+d*(5-cur%5);
			if(cur-cur/5<ieq){
				cs=min(cs,(cur-cur/5-1)*d);
			}
			if(!dist.count(cur/5+1)||dist[cur/5+1]>dis+cs){
				dist[cur/5+1]=dis+cs;
				q.emplace(-(dis+cs),cur/5+1);
			}
		}
		if(cur<=100){
			if(!dist.count(cur+1)||dist[cur+1]>dis+d){
				dist[cur+1]=dis+d;
				q.emplace(-(dis+d),cur+1);
			}
			if(!dist.count(cur-1)||dist[cur-1]>dis+d){
				dist[cur-1]=dis+d;
				q.emplace(-(dis+d),cur-1);
			}
		}
	}
}

int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		solve();
	}
	return 0;
}

Measured memory_bytes: 4456