import java.io.*; import java.util.*; /** * Created by VIET ANH on 27/04/2015. */ public class Main { private static int T; private static int n; private static long l1; private static long r1; private static long a,b,c,d; private static long base; public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out); base=Long.parseLong("4294967296"); T=in.nextInt(); while (T-->0) { n=in.nextInt(); l1=in.nextLong(); r1=in.nextLong(); a=in.nextLong(); b=in.nextLong(); c=in.nextLong(); d=in.nextLong(); long x=l1,y=r1; if (x>y) { long tmp=x; x=y; y=tmp; } long rmin=y; long lmax=x; long l=l1,r=r1; for (int i = 0; i < n-1; i++) { l=(l*a+b)%base; r=(r*c+d)%base; x=l; y=r; if (x>y) { long tmp=x; x=y; y=tmp; } rmin=Math.min(rmin,y); lmax=Math.max(lmax,x); } int lc=0; if (rminy) { long tmp=x; x=y; y=tmp; } if (rmin