#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.tie(nullptr);
  long long int t;
  std::cin >> t;
  while (t--) {
    bool is_succes = true;
    std::string s;
    long long int x;
    std::cin >> s >> x;
    std::string w(s.size(), '1');
    for (long long int i = 0; i < s.size(); ++i) {
      if (s[i] == '0') {
        if (i - x >= 0) w[i - x] = '0';
        if (i + x <= s.size()) w[i + x] = '0';
      }
    }
    for (long long int i = 0; i < s.size(); ++i) {
      if (s[i] == '1') {
        if ((i + x <= w.size() && w[i + x] == '1') ||
            (i - x >= 0 && w[i - x] == '1'))
          continue;
        else {
          std::cout << "-1\n";
          is_succes = false;
          break;
        }
      }
      if (s[i] == '0') {
        if ((i + x <= w.size() && w[i + x] == '1') ||
            (i - x >= 0 && w[i - x] == '1')) {
          std::cout << "-1\n";
          is_succes = false;
          break;
        } else {
          continue;
        }
      }
    }
    if (is_succes) std::cout << w << "\n";
  }
  return 0;
}
