JAVA SOLUTION USING TREESET
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) {
         Map<Integer,TreeSet<Integer>> map=new HashMap<>();
         for(int i=0;i<nums.length;i++){
            map.computeIfAbsent(nums[i],key->new TreeSet<>()).add(i);
         }
         int n=nums.length;
        List<Integer> list=new ArrayList<>();
         int i=0;
         for(int num:queries){
             int val=nums[num];
             if(map.get(val).size()==1) list.add(-1);
             else{
                 Integer idx1=map.get(val).higher(num);
                 Integer idx2=map.get(val).lower(num);
                 Integer idx3=map.get(val).ceiling(0);
                 Integer idx4=map.get(val).floor(n-1);
                 int t1=Integer.MAX_VALUE;
                 int t2=Integer.MAX_VALUE;
                 if(idx1!=null) idx1=Math.min(idx1-num,num+n-idx1);
                 if(idx2!=null) idx2=Math.min(num-idx2,n-num+(idx2));
                 if(idx1!=null) t1=idx1;
                 if(idx2!=null) t2=idx2;
                 if(idx3!=null) t1=Math.min(t1,n-num+idx3);
                 if(idx4!=null) t2=Math.min(t2,num+n-idx4);
                 System.out.println(val+" "+idx1+" "+idx2);
                 list.add(Math.min(t1,t2));

                

             }

         }
         return list;
    }
}
```
['Java']