import sys
import math
MAX_VAL = 500005
temp_counts = [0] * MAX_VAL
def calculate_beauty_direct(a, l, r):
for i in range(len(temp_counts)):
temp_counts[i] = 0
for i in range(l, r + 1):
temp_counts[a[i]] += 1
beauty = 0
# In the original C++, the loop limit is N, assuming values are <= N.
# We will iterate only over values present to be safe and efficient.
# To be a 1:1 translation, we would need to know the max possible value in a.
# The below is a slightly more Pythonic way for the same result.
temp_local_counts = {}
for i in range(l, r + 1):
val = a[i]
temp_local_counts[val] = temp_local_counts.get(val, 0) + 1
for val, count in temp_local_counts.items():
if count % 2 != 0:
beauty += val
return beauty
def solve():
try:
line1 = sys.stdin.readline()
if not line1: return None
n, q = map(int, line1.split())
a = list(map(int, sys.stdin.readline().split()))
except (IOError, ValueError):
return None
block_size = max(1, int(math.sqrt(n)))
num_blocks = (n + block_size - 1) // block_size
get_block_id = lambda i: i // block_size
get_block_start = lambda block_idx: block_idx * block_size
get_block_end = lambda block_idx: min(n - 1, (block_idx + 1) * block_size - 1)
block_beauty = [[0] * num_blocks for _ in range(num_blocks)]
max_val_in_a = max(a) if a else 0
for i in range(num_blocks):
for k in range(max_val_in_a + 1): temp_counts[k] = 0
current_beauty = 0
for j in range(i, num_blocks):
start_k = get_block_start(j)
end_k = get_block_end(j)
for k in range(start_k, end_k + 1):
val = a[k]
if temp_counts[val] % 2 != 0:
current_beauty -= val
else:
current_beauty += val
temp_counts[val] += 1
block_beauty[i][j] = current_beauty
ans_prev = 0
answers = []
for _ in range(q):
xp, yp = map(int, sys.stdin.readline().split())
x = (xp - 1 + ans_prev) % n
y = (yp - 1 + ans_prev) % n
l = min(x, y)
r = max(x, y)
bl = get_block_id(l)
br = get_block_id(r)
current_beauty = 0
if bl == br:
current_beauty = calculate_beauty_direct(a, l, r)
else:
for k in range(max_val_in_a + 1): temp_counts[k] = 0
if bl + 1 <= br - 1:
current_beauty = block_beauty[bl + 1][br - 1]
l_full = get_block_start(bl + 1)
r_full = get_block_end(br - 1)
for k in range(l_full, r_full + 1):
temp_counts[a[k]] += 1
r_bl = get_block_end(bl)
for k in range(l, r_bl + 1):
val = a[k]
if temp_counts[val] % 2 != 0:
current_beauty -= val
else:
current_beauty += val
temp_counts[val] += 1
l_br = get_block_start(br)
for k in range(l_br, r + 1):
val = a[k]
if temp_counts[val] % 2 != 0:
current_beauty -= val
else:
current_beauty += val
temp_counts[val] += 1
answers.append(str(current_beauty))
ans_prev = current_beauty
return " ".join(answers)
def main():
try:
t_line = sys.stdin.readline()
if not t_line: return
t = int(t_line)
output_buffer = []
for _ in range(t):
result = solve()
if result is not None:
output_buffer.append(result)
print("\n".join(output_buffer))
except (IOError, ValueError):
return
if __name__ == "__main__":
main()