#!/usr/bin/env python3 n, q, m = map(int, input().split()) p = list(map(int, input().split())) t = list(map(lambda x: int(x) + 1, input().split())) # 'hop': moving between countries without quarantine, usually from a place j to a place i < j, such that p_j <= p_i + m. # 'jump': moving between two arbitrary countries, typically with quarantine. # hop[l][i] Which place we land after 2^l steps to the right starting at i. # This is needed to compute the number of hops needed when we do not quarantine. L = 18 hop = [[0] * n for _ in range(L)] # Initialize the first hop. r = 0 for i in range(n): while r < n - 1 and p[r + 1] <= p[i] + m: r += 1 hop[0][i] = r # Do the recursion for the next layers. for l in range(1, L): for i in range(n): hop[l][i] = hop[l - 1][hop[l - 1][i]] # The number of hops to go from j to i, or inf if this is impossible. inf = 2 * 10**9 def num_hops(i, j): if i > j: i, j = j, i if hop[-1][i] < j: return inf h = 0 for l in range(L - 1, -1, -1): if hop[l][i] < j: h += 2**l i = hop[l][i] assert i < j i = hop[0][i] h += 1 assert i >= j return h # For each position, find the minimal cost of getting there given that you quarantine. # First find the cost when jumping to a place with lower p and then jumping up. jumpleft = [0] * n best = t[0] for i in range(0, n): jumpleft[i] = min(t[i], best + 1) best = min(t[i], best) # Next find the cost when jumping and then taking some additional hops to the left. jumpright = [0] * n jumpright[n - 1] = t[n - 1] best = n - 1 for i in range(n - 2, -1, -1): h = num_hops(i, best) if h == inf: best = i jumpright[i] = t[i] continue viabest = num_hops(i, best) + jumpright[best] if t[i] < viabest: jumpright[i] = t[i] best = i else: jumpright[i] = viabest # Answer all queries. for _ in range(q): x, y = map(int, input().split()) x -= 1 y -= 1 # Given assert x != y # For free? if x < y: print(1) continue h, l, r = num_hops(x, y), jumpleft[y], jumpright[y] # print(h, l, r) print(min(h, l, r))