#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 T, mod, numRow, numCol, rowProductMod, sumMod, row, col, num;
  scanf("%d", &T);
  while (T) {
    scanf("%d %d", &mod, &numRow);
    sumMod = 0;
    for (row = 0; row < numRow; row++) {
      rowProductMod = 1;
      scanf("%d", &numCol);
      for (col = 0; col < numCol; col++) {
        scanf("%d", &num);
        rowProductMod = (rowProductMod * num % mod) % mod;
      }
      sumMod = (sumMod + rowProductMod) % mod;
    }
    printf("%d\n", sumMod);
    T--;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xecd(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpl   $0x0,-0x8(%rbp)
je     1213 <main+0xf3>
lea    0xeae(%rip),%rdi
lea    -0xc(%rbp),%rsi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    11f6 <main+0xd6>
movl   $0x1,-0x18(%rbp)
lea    0xe7a(%rip),%rdi
lea    -0x14(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11db <main+0xbb>
lea    0xe55(%rip),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
mov    -0x18(%rbp),%eax
imul   -0x28(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119b <main+0x7b>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
cltd
idivl  -0xc(%rbp)
mov    %edx,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    116f <main+0x4f>
mov    -0x1c(%rbp),%esi
lea    0xe06(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
jmp    1141 <main+0x21>
xor    %eax,%eax
add    $0x30,%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    0x10(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x10(%rsp)
je     121d <main+0xfd>
lea    0xeb0(%rip),%rbx
lea    0x14(%rsp),%rbp
jmp    117a <main+0x5a>
nopw   0x0(%rax,%rax,1)
lea    0xe9f(%rip),%rdi
mov    %r15d,%esi
call   1030 <printf@plt>
addl   $0xffffffff,0x10(%rsp)
je     121d <main+0xfd>
lea    0xe7f(%rip),%rdi
lea    0xc(%rsp),%rsi
lea    0x8(%rsp),%rdx
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x8(%rsp)
mov    $0x0,%r15d
jle    1160 <main+0x40>
xor    %r14d,%r14d
xor    %r15d,%r15d
jmp    11d1 <main+0xb1>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%r12d
mov    0xc(%rsp),%ecx
add    %r15d,%r12d
mov    %r12d,%eax
cltd
idiv   %ecx
add    $0x1,%r14d
mov    %edx,%r15d
cmp    0x8(%rsp),%r14d
jge    1160 <main+0x40>
mov    %rbx,%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    11b0 <main+0x90>
mov    $0x1,%r12d
xor    %r13d,%r13d
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
imul   0x14(%rsp),%r12d
mov    0xc(%rsp),%ecx
mov    %r12d,%eax
cltd
idiv   %ecx
add    $0x1,%r13d
mov    %edx,%r12d
cmp    0x4(%rsp),%r13d
jl     11f0 <main+0xd0>
jmp    11ba <main+0x9a>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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