Linear time and space, 2 passes
Complexity

Time complexity:
O(n+q)


Space complexity:
O(n+q)

Code
# Complexity
- Time complexity:
O(n+q)

- Space complexity:
O(n+q)

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        
        # Solution: 2 passes (or technically 4 passes)
        # For each index in nums, find the closest occurrence to both the 'left' and 'right'
        # For each, take the minimum if we go clockwise vs counterclockwise to reach it
        # Note to account for the case when the closest left/right goes over the edge of the array,
        # we need a second pass using the first/last occurrence of each number
        # Each pass takes O(n) time
        # After solving for all indices in nums, each query takes O(1) time
        # Thus the TC and SC is O(n+q)

        # Setup
        min_dist = [float('inf')] * len(nums)
        last_seen_left, last_seen_right = {}, {}
        res = []

        # First pass/passes
        for i in range(len(nums)):
            if nums[i] in last_seen_left:
                min_dist[i] = min(min_dist[i], i - last_seen_left[nums[i]], len(nums) - i + last_seen_left[nums[i]])
            last_seen_left[nums[i]] = i

            j = len(nums)-1-i
            if nums[j] in last_seen_right:
                min_dist[j] = min(min_dist[j], last_seen_right[nums[j]] - j, len(nums) + j - last_seen_right[nums[j]])
            last_seen_right[nums[j]] = j
        
        # Second pass/passes
        for i in range(len(nums)):
            if last_seen_right[nums[i]] < i: min_dist[i] = min(min_dist[i], i - last_seen_right[nums[i]], len(nums) - i + last_seen_right[nums[i]])
            if last_seen_left[nums[i]] > i: min_dist[i] = min(min_dist[i], last_seen_left[nums[i]] - i, len(nums) + i - last_seen_left[nums[i]])

        


        # Solve
        for q in queries:
            res.append(min_dist[q] if min_dist[q] < len(nums) else -1)
        return res

```
['Python3']