sorted list of indices for every value x + binary_search
```rust []
use std::collections::HashMap;

impl Solution {
    pub fn solve_queries(nums: Vec<i32>, queries: Vec<i32>) -> Vec<i32> {
        let n = nums.len() as i32;

        let m  = nums.iter().enumerate()
           .fold(HashMap::<i32, Vec<usize>>::new(), |mut m, (i, &x)| {
            m.entry(x).or_default().push(i);
            m
        });
        queries.into_iter().map(|i|
            { let i = i as usize;
              let x = nums[i];
              let v = &m[&x];
              let k = v.len();
              if k == 1 {
                -1
              } else {
                  let j = m[&x].binary_search(&i).ok().unwrap();
                  let pre = v[(j + k - 1).rem_euclid(k)];
                  let nxt = v[(j+1).rem_euclid(k)];
                  let d1 = i as i32 - pre as i32;
                  let d2 = i as i32 - nxt as i32;
                  [d1, -d1, d2, -d2].into_iter().map(|d|d.rem_euclid(n)).min().unwrap()
              }
            }
        ).collect::<Vec<_>>()
    }
}
```
['Rust']