#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 chefQueries(int, int, int, int, int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *, ...);
int main() {
  int n, i, q, j = 0, l, r, x, y;
  int *arr;
  scanf("%d", &n);
  arr = (int *)malloc(n * sizeof(int));
  for (i = 0; i < n; i++) {
    scanf("%d", &arr[i]);
  }
  scanf("%d", &q);
  while (j++ < q) {
    scanf("%d%d%d%d", &l, &r, &x, &y);
    printf("%u\n", chefQueries(l - 1, r - 1, x, y, arr));
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xea9(%rip),%rdi
lea    -0x8(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
movslq -0x8(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11b9 <main+0x79>
mov    -0x30(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xe62(%rip),%rdi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1182 <main+0x42>
lea    0xe46(%rip),%rdi
lea    -0x10(%rbp),%rsi
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x14(%rbp)
cmp    -0x10(%rbp),%eax
jge    122b <main+0xeb>
lea    0xe1a(%rip),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
mov    $0x0,%al
call   1060 <scanf@plt>
mov    -0x18(%rbp),%edi
sub    $0x1,%edi
mov    -0x1c(%rbp),%esi
sub    $0x1,%esi
mov    -0x20(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x30(%rbp),%r8
call   1030 <chefQueries@plt>
mov    %eax,%esi
lea    0xde8(%rip),%rdi
call   1040 <printf@plt>
jmp    11cb <main+0x8b>
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    0xeb1(%rip),%rdi
mov    %rsp,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
mov    (%rsp),%edi
shl    $0x2,%edi
call   1050 <malloc@plt>
mov    %rax,%r15
cmpl   $0x0,(%rsp)
jle    119e <main+0x5e>
lea    0xe8c(%rip),%r14
mov    %r15,%rbx
xor    %ebp,%ebp
nop
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
add    $0x1,%rbp
movslq (%rsp),%rax
add    $0x4,%rbx
cmp    %rax,%rbp
jl     1180 <main+0x40>
lea    0xe61(%rip),%rdi
lea    0x4(%rsp),%rsi
xor    %eax,%eax
call   1060 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
jle    121d <main+0xdd>
lea    0x10(%rsp),%r12
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%rbp
lea    0xe3b(%rip),%r14
xor    %ebx,%ebx
add    $0x1,%ebx
lea    0xe26(%rip),%rdi
lea    0x14(%rsp),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1060 <scanf@plt>
mov    0x14(%rsp),%edi
add    $0xffffffff,%edi
mov    0x10(%rsp),%esi
add    $0xffffffff,%esi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    %r15,%r8
call   1030 <chefQueries@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
cmp    0x4(%rsp),%ebx
jl     11d0 <main+0x90>
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 = [9 x i8] c"%d%d%d%d\00"
@.str.2 = [4 x i8] c"%u\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
store i32 0, i32* %5
%11 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %2)
%12 = load i32, i32* %2
%13 = sext i32 %12 to i64
%14 = mul i64 %13, 4
%15 = trunc i64 %14 to i32
%16 = call i64 @malloc(i32 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %10
store i32 0, i32* %3
br label %18
18: ; preds = %28, %0
%19 = load i32, i32* %3
%20 = load i32, i32* %2
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i32*, i32** %10
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %26)
br label %28
28: ; preds = %22
%29 = load i32, i32* %3
%30 = add nsw i32 %29, 1
store i32 %30, i32* %3
br label %18
31: ; preds = %18
%32 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %4)
br label %33
33: ; preds = %38, %31
%34 = load i32, i32* %5
%35 = add nsw i32 %34, 1
store i32 %35, i32* %5
%36 = load i32, i32* %4
%37 = icmp slt i32 %34, %36
br i1 %37, label %38, label %49
38: ; preds = %33
%39 = call i32 (i8*, i32*, ...) @scanf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32* %6, i32* %7, i32* %8, i32* %9)
%40 = load i32, i32* %6
%41 = sub nsw i32 %40, 1
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, 1
%44 = load i32, i32* %8
%45 = load i32, i32* %9
%46 = load i32*, i32** %10
%47 = call i32 @chefQueries(i32 %41, i32 %43, i32 %44, i32 %45, i32* %46)
%48 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %47)
br label %33
49: ; preds = %33
ret i32 0
}

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