fn main() { let t: usize = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); line.trim().parse().unwrap() }; let INV=vec![0,1,5,0,7,2,0,4,8]; for tests in 0..t{ let (n,x,a,b,m): (usize, i32, i32, i32, i32) = { let mut line: String = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let mut iter = line.split_whitespace(); ( iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap(), iter.next().unwrap().parse().unwrap() ) }; let mut now=x; let mut flag0=0; if now%10==0{ flag0=1; } let mut ans=0; let mut two: i32=1; let mut three=0; for i in 0..n{ if i==0{ ans=(ans+now%10)%9; continue; } now=((now^a)+b)%m; if (now%10)!=0{ flag0=0; } if i==n-1{ ans=(ans+now%10)%9; continue; } let mut x:i32=i as i32; while (x%3==0){ x/=3; three-=1; } two=two*INV[(x%9) as usize]%9; x=(n-i) as i32; while (x%3==0){ x/=3; three+=1; } two=two*x%9; if three==0{ ans=(ans+(now%10)*two)%9; } else if three==1{ ans=(ans+(now%10)*two*3)%9; } } if flag0==1{ println!("0"); continue; } if ans==0{ ans=9; } println!("{}", ans); } }