Java O(N + QlogN)
IntuitionApproachComplexity
Time complexity:

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

# 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
```java []
class Solution {
    public List<Integer> solveQueries(int[] nums, int[] queries) {
        int n = nums.length;
        Map<Integer, List<Integer>> hs = new HashMap<>();
        
        for (int i = 0; i < n; i++) {
            hs.computeIfAbsent(nums[i], k -> new ArrayList<>()).add(i);
        }
        
        List<Integer> result = new ArrayList<>();
        for (int ni : queries) {
            int nv = nums[ni];
            List<Integer> indices = hs.get(nv);
            
            if (indices.size() <= 1) {
                result.add(-1);
                continue;
            }
            
            int min = n;
            
            int position = Collections.binarySearch(indices, ni);
            
            int prev = (position - 1 + indices.size()) % indices.size();
            int next = (position + 1) % indices.size();
            
            int dPrev = Math.min(
                Math.abs(ni - indices.get(prev)), 
                n - Math.abs(ni - indices.get(prev))
            );
            
            int dNext = Math.min(
                Math.abs(ni - indices.get(next)), 
                n - Math.abs(ni - indices.get(next))
            );
            
            min = Math.min(dPrev, dNext);
            
            result.add(min);
        }
        
        return result;
    }
}
```
['Java']