#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  long long t;
  cin >> t;
  while (t--) {
    string s;
    long long x;
    cin >> s;
    cin >> x;
    long long n = s.length();
    long long arr[n];
    for (long long i = 0; i < n; i++) {
      arr[i] = 1;
    }
    for (long long i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i >= x) {
          arr[i - x] = 0;
        }
        if ((i + x) < n) {
          arr[i + x] = 0;
        }
      }
    }
    long long ar[n];
    for (long long i = 0; i < n; i++) {
      if ((i - x) >= 0 && arr[i - x] == 1) {
        ar[i] = 1;
      } else if ((i + x) < n && arr[i + x] == 1) {
        ar[i] = 1;
      } else {
        ar[i] = 0;
      }
    }
    long long k = 0;
    for (long long i = 0; i < n; i++) {
      if (s[i] != '0' && ar[i] == 0) {
        cout << "-1" << ("\n");
        k = 1;
        break;
      } else if (s[i] == '0' && ar[i] != 0) {
        cout << "-1" << ("\n");
        k = 1;
        break;
      }
    }
    if (k != 1) {
      for (long long i = 0; i < n; i++) {
        cout << arr[i];
      }
      cout << ("\n");
    }
  }
  return 0;
}
