#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    string w;
    int len = s.length();
    for (int i = 0; i < len; i++) w += "1";
    for (int i = 0; i < len; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) w[i - x] = '0';
        if (i + x < len) w[i + x] = '0';
      }
    }
    for (int i = 0; i < len; i++) {
      if (s[i] == '1') {
        if (i - x >= 0 && w[i - x] == '1')
          continue;
        else if (i + x < len && w[i + x] == '1')
          continue;
        else {
          w = "-1";
          break;
        }
      }
    }
    cout << w << "\n";
  }
  cerr << "time taken : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl;
  return 0;
}
