PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000013. Cuộc đột kích đàn bò (Time to Raid Cowavans)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cuộc đột kích đàn bò (Time to Raid Cowavans)

Time to Raid Cowavans

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn cows in a row, numbered from 11 to nn. Cow ii has weight wiw_i. The aliens investigate pp independent raiding plans, each defined by positive integers a,ba,b.

For plan (a,b)(a,b), they select cows at positions

a, a+b, a+2b,…a,\ a+b,\ a+2b,\ldots

as long as the position does not exceed nn. Find the sum of weights of all selected cows. Every plan is a separate query: cows are not removed and indices are never renumbered. If a=na=n, only one cow is selected.

Input

The first line contains nn; the second contains nn weights wiw_i; the third contains pp. The next pp lines contain a b.

Output

Output the total weight for each plan on its own line, in input order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤801\le n\le 80; 1≤p≤1001\le p\le 100; 1≤wi≤1091\le w_i\le 10^9; 1≤a,b≤n1\le a,b\le n.
  • Subtask 2 (15 points): 1≤n≤30001\le n\le 3000; 1≤p≤30001\le p\le 3000; 1≤wi≤1091\le w_i\le 10^9; 1≤a,b≤n1\le a,b\le n.
  • Subtask 3 (30 points): 1≤n≤300001\le n\le 30000; 1≤p≤300001\le p\le 30000; 1≤wi≤1091\le w_i\le 10^9; 1≤a,b≤n1\le a,b\le n.
  • Subtask 4 (45 points): 1≤n≤3000001\le n\le 300000; 1≤p≤3000001\le p\le 300000; 1≤wi≤1091\le w_i\le 10^9; 1≤a,b≤n1\le a,b\le n.

Examples

Input

4
2 3 5 7
3
1 3
2 3
2 2

Output

9
3
10

Explanation

Plan (1,3) takes positions 1,41,4, giving 2+7=92+7=9. Plan (2,3) takes only position 22, giving 33. Plan (2,2) takes positions 2,42,4, giving 3+7=103+7=10.