#include <bits/stdc++.h>
using namespace std;
int n, x, w[100003];
string s;
void in() {
  cin >> s >> x;
  n = s.length();
  s = " " + s;
}
void xuly() {
  memset(w, -1, sizeof w);
  for (int i = 1; i <= n; i++)
    if (s[i] == '0') {
      if (i > x) w[i - x] = 0;
      if (i + x <= n) w[i + x] = 0;
    }
  for (int i = 1; i <= n; i++)
    if (s[i] == '1') {
      bool check = false;
      if (i > x)
        if (w[i - x] != 0) check = true, w[i - x] = 1;
      if (i + x <= n && w[i + x] != 0) check = true, w[i + x] = 1;
      if (!check) {
        cout << -1 << '\n';
        return;
      }
    }
  for (int i = 1; i <= n; i++) {
    if (w[i] == -1) w[i] = 1;
    cout << w[i];
  }
  cout << '\n';
}
int main() {
  int T;
  cin >> T;
  while (T--) {
    in();
    xuly();
  }
}
