#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  long long n;
  cin >> n;
  long long m = s.size();
  char c[m];
  for (long long i = 0; i < m; i++) {
    c[i] = '1';
  }
  for (long long i = 0; i < m; i++)
    if (s[i] == '0') {
      if (i - n >= 0) c[i - n] = '0';
      if (i + n < m) c[i + n] = '0';
    }
  long long flag = 0;
  for (long long i = 0; i < m; i++) {
    if (s[i] == '1') {
      if ((i - n >= 0 && c[i - n] == '1') || (i + n < m && c[i + n] == '1')) {
      } else {
        flag = 1;
        break;
      }
    }
  }
  if (flag == 1)
    cout << "-1";
  else
    for (long long i = 0; i < m; i++) cout << c[i];
}
signed main() {
  long long n;
  cin >> n;
  while (n--) {
    solve();
    cout << endl;
  }
}
