#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long x;
    cin >> x;
    long long n = s.size();
    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';
      }
    }
    string res(n, '0');
    for (long long i = 0; i < n; ++i) {
      long long l = i - x;
      long long r = i + x;
      if (l >= 0) {
        if (ans[l] == '1') res[i] = '1';
      }
      if (r < n) {
        if (ans[r] == '1') res[i] = '1';
      }
    }
    if (res == s)
      cout << ans << "\n";
    else
      cout << -1 << "\n";
  }
}
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
  }
  solve();
  return 0;
}
