#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t, x;
  cin >> t;
  for (int i = 0; i < t; i++) {
    string s;
    cin >> s >> x;
    bool possible = true;
    for (int j = 0; j < s.size(); j++) {
      if (s[j] == '1') {
        possible = false;
        if (j > x - 1) {
          if (j < 2 * x or s[j - 2 * x] == '1') possible = true;
        }
        if (j < s.size() - x) {
          if (j > (int)s.size() - 2 * x - 1 or s[j + 2 * x] == '1')
            possible = true;
        }
        if (!possible) {
          break;
        }
      }
    }
    if (!possible) {
      cout << -1 << "\n";
    } else {
      for (int j = 0; j < s.size(); j++) {
        if ((j > x - 1 and s[j - x] == '0') or
            (j < s.size() - x and s[j + x] == '0')) {
          cout << 0;
        } else {
          cout << 1;
        }
      }
      cout << "\n";
    }
  }
}
