#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main(void) {
  int a, b, c, d, e, n, sum;
  scanf("%d\n", &a);
  while (a-- > 0) {
    sum = 0;
    scanf("%d %d\n", &b, &c);
    while (c-- > 0) {
      n = 1;
      scanf("%d ", &d);
      while (d-- > 0) {
        scanf("%d ", &e);
        n *= e;
        n %= b;
      }
      sum += n;
    }
    printf("%d\n", sum % b);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xecd(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x8(%rbp)
cmp    $0x0,%eax
jle    1208 <main+0xe8>
movl   $0x0,-0x20(%rbp)
lea    0xe9d(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x10(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x10(%rbp)
cmp    $0x0,%eax
jle    11ee <main+0xce>
movl   $0x1,-0x1c(%rbp)
lea    0xe73(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
jle    11e0 <main+0xc0>
lea    0xe4d(%rip),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x18(%rbp),%eax
imul   -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x1c(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x1c(%rbp)
jmp    119f <main+0x7f>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1172 <main+0x52>
mov    -0x20(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%esi
lea    0xe05(%rip),%rdi
call   1030 <printf@plt>
jmp    1141 <main+0x21>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xece(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
test   %eax,%eax
jle    123f <main+0x11f>
lea    0xeac(%rip),%rbx
lea    0x4(%rsp),%r13
lea    0x14(%rsp),%rbp
lea    0xe97(%rip),%r14
jmp    1195 <main+0x75>
xchg   %ax,%ax
mov    %r15d,%eax
cltd
idivl  0x10(%rsp)
mov    %r14,%rdi
mov    %edx,%esi
call   1030 <printf@plt>
mov    0xc(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0xc(%rsp)
test   %eax,%eax
jle    123f <main+0x11f>
lea    0xe64(%rip),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
mov    $0x0,%r15d
test   %eax,%eax
jle    1170 <main+0x50>
xor    %r15d,%r15d
jmp    11e2 <main+0xc2>
nopw   0x0(%rax,%rax,1)
add    %r12d,%r15d
mov    0x8(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x8(%rsp)
test   %eax,%eax
jle    1170 <main+0x50>
mov    %rbx,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
mov    $0x1,%r12d
test   %eax,%eax
jle    11d0 <main+0xb0>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
imul   0x14(%rsp),%r12d
mov    %r12d,%eax
cltd
idivl  0x10(%rsp)
mov    0x4(%rsp),%eax
lea    -0x1(%rax),%ecx
mov    %ecx,0x4(%rsp)
mov    %edx,%r12d
test   %eax,%eax
jg     1210 <main+0xf0>
jmp    11d0 <main+0xb0>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [7 x i8] c"%d %d\0A\00"
@.str.2 = [4 x i8] c"%d \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
br label %10
10: ; preds = %38, %0
%11 = load i32, i32* %2
%12 = add nsw i32 %11, -1
store i32 %12, i32* %2
%13 = icmp sgt i32 %11, 0
br i1 %13, label %14, label %43
14: ; preds = %10
store i32 0, i32* %8
%15 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %3, i32* %4)
br label %16
16: ; preds = %34, %14
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
store i32 %18, i32* %4
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %38
20: ; preds = %16
store i32 1, i32* %7
%21 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %5)
br label %22
22: ; preds = %26, %20
%23 = load i32, i32* %5
%24 = add nsw i32 %23, -1
store i32 %24, i32* %5
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %6)
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = mul nsw i32 %29, %28
store i32 %30, i32* %7
%31 = load i32, i32* %3
%32 = load i32, i32* %7
%33 = srem i32 %32, %31
store i32 %33, i32* %7
br label %22
34: ; preds = %22
%35 = load i32, i32* %7
%36 = load i32, i32* %8
%37 = add nsw i32 %36, %35
store i32 %37, i32* %8
br label %16
38: ; preds = %16
%39 = load i32, i32* %8
%40 = load i32, i32* %3
%41 = srem i32 %39, %40
%42 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %41)
br label %10
43: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
@.str.1 = [7 x i8] c"%d %d\0A\00"
@.str.2 = [4 x i8] c"%d \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %1 to i8*
%7 = bitcast i32* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %1)
%12 = load i32, i32* %1
%13 = add nsw i32 %12, -1
store i32 %13, i32* %1
%14 = icmp sgt i32 %12, 0
br i1 %14, label %15, label %50
15: ; preds = %0, %42
%16 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32* %2, i32* %3)
%17 = load i32, i32* %3
%18 = add nsw i32 %17, -1
store i32 %18, i32* %3
%19 = icmp sgt i32 %17, 0
br i1 %19, label %20, label %42
20: ; preds = %15, %36
%21 = phi i32 [ %38, %36 ], [ 0, %15 ]
%22 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%23 = load i32, i32* %4
%24 = add nsw i32 %23, -1
store i32 %24, i32* %4
%25 = icmp sgt i32 %23, 0
br i1 %25, label %26, label %36
26: ; preds = %20, %26
%27 = phi i32 [ %32, %26 ], [ 1, %20 ]
%28 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%29 = load i32, i32* %5
%30 = mul nsw i32 %29, %27
%31 = load i32, i32* %2
%32 = srem i32 %30, %31
%33 = load i32, i32* %4
%34 = add nsw i32 %33, -1
store i32 %34, i32* %4
%35 = icmp sgt i32 %33, 0
br i1 %35, label %26, label %36
36: ; preds = %26, %20
%37 = phi i32 [ 1, %20 ], [ %32, %26 ]
%38 = add nsw i32 %37, %21
%39 = load i32, i32* %3
%40 = add nsw i32 %39, -1
store i32 %40, i32* %3
%41 = icmp sgt i32 %39, 0
br i1 %41, label %20, label %42
42: ; preds = %36, %15
%43 = phi i32 [ 0, %15 ], [ %38, %36 ]
%44 = load i32, i32* %2
%45 = srem i32 %43, %44
%46 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %1
%48 = add nsw i32 %47, -1
store i32 %48, i32* %1
%49 = icmp sgt i32 %47, 0
br i1 %49, label %15, label %50
50: ; preds = %42, %0
ret i32 0
}