#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
string func(string s, long long x) {
  string res;
  for (long long i = 0; i < s.size(); i++) {
    if (i - x >= 0 && s[i - x] == '1' || i + x < s.size() && s[i + x] == '1')
      res += '1';
    else
      res += '0';
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long x, n = s.size();
    cin >> x;
    string ans(n, '1');
    for (long long i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) ans[i - x] = '0';
        if (i + x < n) ans[i + x] = '0';
      }
    }
    if (func(ans, x) == s)
      cout << ans;
    else
      cout << -1;
    cout << "\n";
  }
  return 0;
}
