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