Mehta is a very rich guy. He has $N$ types of coins, and each type of coin is available in an unlimited supply.  

So Mehta goes to a supermarket to buy monthly groceries. There he sees that every item has a unique price, that is, no two items have the same price.  

Now, the supermarket owner tells Mehta that they are selling items in the price range $[L,R]$ only on that particular day. He also tells Mehta that for every price, there is an item in the shop.  

The supermarket has recently adopted a weird new tradition: Mehta may only use a single type of coin for each item he purchases. For example, he could pay for an item of price 4 with two 2-coins, but not with a 3-coin and a 1-coin.  

As you know Mehta is very weak at calculations, so he wants you to do these calculations for him and tell how many different types of items he can buy.  

Input Format 

The first line of input contains $N$, the number of types of coins Mehta has. 

Then the next $N$ lines contain an integer each: the i^{th} line contains $A[i]$, the value of Mehta's i^{th} type of coin.  

Then the next line contains a number $\mbox{D}$, the number of days Mehta goes shopping. 

Then each of the next $\mbox{D}$ lines contains numbers $\boldsymbol{\mbox{L}}$ and $\mbox{R}$, denoting that they are selling items in price range $[L,R]$ on that particular day.  

Output format 

There will be $\mbox{D}$ lines, each containing the number of distinct items that can be bought at that particular day.  

Constraints 

$1\leq N\leq17$ 

$1\leq A[i]\leq51$ 

$1\leq D\leq101$ 

$1\leq L\leq R\leq10^{18}$  

Sample Input  

4
2
3
4
5
3
1 10
2 20
3 7

Sample output

8
14
4

Explanation 

For $L=1$ and $R=10$ you can buy items of price $\{2,3,4,5,6,8,9,10\}$. 

For $L=2$ and $\textbf{R}=20$ you can buy items of price $\text{{2,3,4,5,6,8,9,10,12,14,15,16,18,20}}$. 

For $L=3$ and $R=7$ you can buy items of price $\{3,4,5,6\}$.

Difficulty: MEDIUM_HARD

Solutions:
import math

def lcm(F):
	lcmFactors = dict(F[0])
	for f in F[1:]:
		for (n, c) in f.items():
			lcmFactors[n] = max(lcmFactors.get(n, 0), c)
	result = 1
	for (n, c) in lcmFactors.items():
		result *= n ** c
	return result

def factorize(N):
	F = dict()
	while N > 1:
		if N % 2 == 0:
			F[2] = F.get(2, 0) + 1
			N = N // 2
		else:
			for x in range(3, int(math.sqrt(N) + 1), 2):
				if N % x == 0:
					F[x] = F.get(x, 0) + 1
					N = N // x
					break
			else:
				F[N] = F.get(N, 0) + 1
				N = 1
	return F

def f(A, R, F):
	total = 0
	for n in range(1, 2 ** len(A)):
		t = n
		multiplier = -1
		divisor = 1
		factorList = list()
		for i in range(0, len(A)):
			if t & 1:
				factorList.append(F[i])
				multiplier *= -1
			t = t >> 1
			if not t:
				break
		divisor = lcm(factorList)
		common = R // divisor
		total += common * multiplier
	return total

def solve(A, L, R):
	divisors = list()
	factors = list()
	for x in sorted(A):
		for d in divisors:
			if x % d == 0:
				break
		else:
			divisors.append(x)
			factors.append(factorize(x))
	return f(divisors, R, factors) - f(divisors, L - 1, factors)
A = list()
for _ in range(0, int(input())):
	A.append(int(input()))
for _ in range(0, int(input())):
	print(solve(A, *[int(x) for x in input().split(' ')]))


def gcd(a, b):
	if b == 0:
		return a
	return gcd(b, a % b)

def lcm(a, b):
	return b // gcd(a, b) * a

def number_of_items(x, a):
	if len(a) == 0:
		return 0
	n = len(a)
	k = []
	for i in range(n):
		k.append([])
	sum = 0
	for i in range(n):
		ai = a[i]
		res = x // ai
		sum += res - number_of_items(res, k[i])
		for j in range(i + 1, n):
			mygcd = ai // gcd(ai, a[j])
			k[j].append(mygcd)
	return sum
n = int(input())
a = []
for i in range(n):
	ai = int(input())
	a.append(ai)
a = sorted(a, reverse=True)
b = []
for i in range(n):
	flag = True
	for j in range(i + 1, n):
		if a[i] % a[j] == 0:
			flag = False
			break
	if flag:
		b.append(a[i])
b.reverse()
d = int(input())
l = []
r = []
for i in range(d):
	(li, ri) = [int(item) for item in input().strip().split()]
	print(number_of_items(ri, b) - number_of_items(li - 1, b))


from math import gcd

def lcm(a, b):
	return a * b // gcd(a, b)
n = int(input())
coin = [int(input()) for i in range(n)]
subs = [[0, 0] for i in range(1 << n)]
for i in range(n):
	subs[1 << i] = [coin[i], 1]
for i in range(1, n + 1):
	for j in range(1, 1 << n):
		if subs[j][1] == i:
			for k in range(n):
				if j & 1 << k == 0:
					subs[j | 1 << k] = [lcm(subs[j][0], coin[k]), i + 1]
subs.sort()
rv = []
curr = subs[1][0]
count = 2 * (subs[1][1] % 2) - 1
for i in range(2, len(subs)):
	if subs[i][0] == curr:
		count += 2 * (subs[i][1] % 2) - 1
	else:
		if count != 0:
			rv.append(curr)
			rv.append(count)
		curr = subs[i][0]
		count = 2 * (subs[i][1] % 2) - 1
rv.append(curr)
rv.append(count)
T = int(input())
for run in range(T):
	(a, b) = [int(i) for i in input().split()]
	c = 0
	for i in range(0, len(rv), 2):
		val = (b - a + 1) // rv[i]
		if (b - a + 1) % rv[i] > 0 and (a % rv[i] == 0 or a % rv[i] > (a - 1 + (b - a + 1) % rv[i]) % rv[i]):
			val += 1
		c += rv[i + 1] * val
	print(c)


def gcd(a, b):
	while b > 0:
		(a, b) = (b, a % b)
	return a

def lcm(a, b):
	return a * b // gcd(a, b)

def lcmSubset(mask, vals):
	ans = 1
	for i in range(len(vals)):
		if mask & 1 << i:
			ans = lcm(ans, vals[i])
	return ans

def getCoins():
	n = int(input())
	vals = []
	for i in range(n):
		vals.append(int(input()))
	return vals

def makeLcms(vals):
	lcms = []
	for i in range(1 << len(vals)):
		lcms.append(lcmSubset(i, vals))
	return lcms

def getSigns(n):
	pars = []
	for i in range(1 << n):
		a = -1
		for x in range(n):
			if i & 1 << x:
				a *= -1
		pars.append(a)
	return pars

def countOptions(N, coins, lcms, signs):
	ans = 0
	for i in range(1, 1 << len(coins)):
		ans += signs[i] * (N // lcms[i])
	return ans
coins = getCoins()
lcms = makeLcms(coins)
signs = getSigns(len(coins))
days = int(input())
for i in range(days):
	ins = input().split(' ')
	L = int(ins[0])
	U = int(ins[1])
	print(countOptions(U, coins, lcms, signs) - countOptions(L - 1, coins, lcms, signs))


import sys
import itertools
from math import gcd

def lcm(x, y):
	lcm = x * y // gcd(x, y)
	return lcm
debug = False
num_coin_types = int(sys.stdin.readline())
coin_types = [int(sys.stdin.readline()) for _ in range(num_coin_types)]
coin_types = sorted(coin_types)
for i in range(len(coin_types) - 1, -1, -1):
	for j in range(i - 1):
		if coin_types[i] % coin_types[j] == 0:
			del coin_types[i]
			break
if debug:
	print(coin_types)

def list_lcm(l):
	r = l[0]
	for e in l:
		r = lcm(r, e)
	return r

def list_prod(l):
	r = 1
	for e in l:
		r *= e
	return r
overlapped_coin_types = []
coin_types_copy = coin_types.copy()
for comb_len in range(2, len(coin_types_copy) + 1, 2):
	overlapped_coin_types.extend([list_lcm(c) for c in itertools.combinations(coin_types_copy, comb_len)])
for comb_len in range(3, len(coin_types_copy) + 1, 2):
	coin_types.extend([list_lcm(c) for c in itertools.combinations(coin_types_copy, comb_len)])
if debug:
	print('coin_types:%s' % coin_types)
	print('overlapped_coin_types:%s' % overlapped_coin_types)
num_days = int(sys.stdin.readline())
price_ranges = []
for i in range(num_days):
	ranges = [int(x) for x in sys.stdin.readline().split()]
	price_ranges.append(ranges)
if False:
	for price_range in price_ranges:
		(low, high) = price_range
		if 1 in coin_types:
			print(high - low + 1)
			continue
		feasible_item_count = 0
		for price in range(low, high + 1):
			for coin_type in coin_types:
				if price % coin_type == 0:
					feasible_item_count += 1
					break
		print(feasible_item_count)
if False:
	for price_range in price_ranges:
		print(price_range)
		(low, high) = price_range
		if 1 in coin_types:
			print(high - low + 1)
			continue
		feasible_item_count = 0
		feasible_price_set = set()
		for coin_type in coin_types:
			if coin_type > high:
				continue
			min_multiplier = int((low - 1) // coin_type) + 1
			max_multiplier = int(high // coin_type)
			feasible_price_set.update([m * coin_type for m in range(min_multiplier, max_multiplier + 1)])
		feasible_item_count += len(feasible_price_set)
		print(feasible_item_count)
if True:
	for price_range in price_ranges:
		(low, high) = price_range
		if 1 in coin_types:
			print(high - low + 1)
			continue
		feasible_item_count = 0
		if debug:
			print('price_range:%s' % price_range)
		for coin_type in coin_types:
			if coin_type > high:
				continue
			cnt1 = (high - high % coin_type) // coin_type
			cnt2 = (low - 1 - (low - 1) % coin_type) // coin_type
			feasible_item_count += cnt1 - cnt2
		for coin_type in overlapped_coin_types:
			if coin_type > high:
				continue
			cnt1 = (high - high % coin_type) // coin_type
			cnt2 = (low - 1 - (low - 1) % coin_type) // coin_type
			feasible_item_count -= cnt1 - cnt2
		print(feasible_item_count)