Recently, the bear started studying data structures and faced the following problem.

You are given a sequence of integers x1, x2, ..., xn of length n and m queries, each of them is characterized by two integers li, ri. Let's introduce f(p) to represent the number of such indexes k, that xk is divisible by p. The answer to the query li, ri is the sum: <image>, where S(li, ri) is a set of prime numbers from segment [li, ri] (both borders are included in the segment).

Help the bear cope with the problem.

Input

The first line contains integer n (1 ≤ n ≤ 106). The second line contains n integers x1, x2, ..., xn (2 ≤ xi ≤ 107). The numbers are not necessarily distinct.

The third line contains integer m (1 ≤ m ≤ 50000). Each of the following m lines contains a pair of space-separated integers, li and ri (2 ≤ li ≤ ri ≤ 2·109) — the numbers that characterize the current query.

Output

Print m integers — the answers to the queries on the order the queries appear in the input.

Examples

Input

6
5 5 7 10 14 15
3
2 11
3 12
4 4


Output

9
7
0


Input

7
2 3 5 7 11 4 8
2
8 10
2 123


Output

0
7

Note

Consider the first sample. Overall, the first sample has 3 queries.

  1. The first query l = 2, r = 11 comes. You need to count f(2) + f(3) + f(5) + f(7) + f(11) = 2 + 1 + 4 + 2 + 0 = 9. 
  2. The second query comes l = 3, r = 12. You need to count f(3) + f(5) + f(7) + f(11) = 1 + 4 + 2 + 0 = 7. 
  3. The third query comes l = 4, r = 4. As this interval has no prime numbers, then the sum equals 0.

Difficulty: MEDIUM_HARD
Tags: Sorting

Implementation

Complete search

Number theory

Dynamic programming

Mathematics

Data structures
Skill types: Dynamic programming

Sorting

Data structures

Complete search

Starter code:

Solutions:
import math, sys
input = sys.stdin.buffer.readline

def ints():
	return map(int, input().split())
n = int(input())
x = list(ints())
MAX = max(x) + 1
freq = [0] * MAX
for i in x:
	freq[i] += 1
sieve = [False] * MAX
f = [0] * MAX
for i in range(2, MAX):
	if sieve[i]:
		continue
	for j in range(i, MAX, i):
		sieve[j] = True
		f[i] += freq[j]
for i in range(2, MAX):
	f[i] += f[i - 1]
m = int(input())
for i in range(m):
	(l, r) = ints()
	if l >= MAX:
		print(0)
	elif r >= MAX:
		print(f[-1] - f[l - 1])
	else:
		print(f[r] - f[l - 1])


import os
from io import BytesIO, IOBase
import sys
from collections import defaultdict, deque, Counter
from math import sqrt, pi, ceil, log, inf, gcd, floor
from itertools import combinations
from bisect import *

def main():
	n = int(input())
	x = list(map(int, input().split()))
	m = int(input())
	z = max(x)
	(i, p, a, ans) = (2, [0] * (z + 1), [], [0] * (z + 1))
	while i * i <= z:
		if not p[i]:
			j = 2 * i
			while j <= z:
				p[j] = i
				j += i
		i += 1
	for i in range(n):
		if not p[x[i]]:
			ans[x[i]] += 1
		else:
			(y, c) = (x[i], set())
			while p[y] != 0 and y != 1:
				c.add(p[y])
				y = y // p[y]
			if not p[y]:
				c.add(y)
			for i in c:
				ans[i] += 1
	for i in range(1, z + 1):
		ans[i] += ans[i - 1]
	for i in range(m):
		(l, r) = map(int, input().split())
		if l > z:
			print(0)
		else:
			r = min(r, z)
			print(ans[r] - ans[l - 1])
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
main()


import sys
input = sys.stdin.buffer.readline
n = int(input())
x = list(map(int, input().split()))
mx = max(x)
hf = [i for i in range(mx + 1)]
p = 2
while p * p <= mx:
	if hf[p] == p:
		for m in range(p * p, mx + 1, p):
			hf[m] = p
	p += 1
count = [0] * (mx + 1)
for e in x:
	while e > 1:
		p = hf[e]
		while e % p == 0:
			e //= p
		count[p] += 1
for i in range(mx):
	count[i + 1] += count[i]
m = int(input())
res = []
for _ in range(m):
	(l, r) = map(int, input().split())
	if l > mx:
		res.append(0)
	else:
		res.append(count[min(r, mx)] - count[l - 1])
print('\n'.join(map(str, res)))


import sys
import io, os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline
import os, sys, atexit
from io import BytesIO, StringIO
_OUTPUT_BUFFER = StringIO()
sys.stdout = _OUTPUT_BUFFER

@atexit.register
def write():
	sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())

def sieve(n):
	prime = [1 for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == 1:
			for i in range(p * p, n + 1, p):
				prime[i] = p
		p += 1
	return prime

def main():
	n = int(input())
	arr = list(map(int, input().split()))
	ma = max(arr) + 1
	sie = sieve(ma)
	l = [0] * (ma + 1)
	for i in range(n):
		curr = arr[i]
		while True:
			if sie[curr] == 1:
				l[curr] += 1
				break
			if curr % (sie[curr] * sie[curr]) != 0:
				l[sie[curr]] += 1
			curr = curr // sie[curr]
	pref = [0] * (ma + 1)
	for i in range(1, len(pref)):
		pref[i] = pref[i - 1] + l[i]
	m = int(input())
	for i in range(m):
		arr = list(map(int, input().split()))
		ls = min(arr[0], ma)
		rs = min(arr[1], ma)
		print(pref[rs] - pref[ls] + l[ls])
main()