#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  int first;
  cin >> s >> first;
  string w = string(s.length(), 'w');
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0') {
      if (i - first >= 0) w[i - first] = '0';
      if (i + first < s.length()) w[i + first] = '0';
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (w[i] == 'w') w[i] = '1';
  }
  string newS = "";
  for (int i = 0; i < s.length(); i++) {
    bool one = false;
    if (i - first >= 0 && w[i - first] == '1') one = true;
    if (i + first < s.length() && w[i + first] == '1') one = true;
    if (one) {
      newS += "1";
    } else
      newS += "0";
  }
  if (s == newS) {
    cout << w << "\n";
  } else {
    cout << "-1\n";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}
