messy but working solution
Intuitionuse binary searchApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
use binary search

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def solveQueries(self, nums: List[int], queries: List[int]) -> List[int]:
        d = defaultdict(SortedList)
        now = set()
        tot = len(nums) 
        for q in queries:
            val = nums[q]
            now.add(val)
        for i, n in enumerate(nums):
            if n in now:
                d[n].add(i)
        ans = []
        for q in queries:
            val = nums[q]
            cur = float('inf')
            options = d[val]
            #print(q, options)
            bl = bisect_left(options, q)
            br = bisect_right(options, q)
            #print(bl - 1, br + 1)
            if len(options) < 100:
                for a in options:
                    if a != q:
                        cur = min(cur, abs(tot - q + a), abs(q - a), abs(tot - a + q))
            else:
                for j in range(bl - 1, br + 1):
                    if j >= 0 and j < len(options):
                        a = options[j]
                        if a != q:
                            cur = min(cur, abs(tot - q + a), abs(q - a), abs(tot - a + q), abs(a - q))
            if cur == float('inf'):
                ans.append(-1)
            else:
                ans.append(cur)
        return ans

            
            
            
```
['Python3']