for _ in[0]*int(input()): n,m,a,b,c,d=map(int,input().split());s=r=0 while(r:=max(r,s,s+a*~-n+b*(n:=(c*~-n+d)//m)))*0