#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    string second;
    cin >> second;
    long long int x;
    cin >> x;
    long long int n = second.size();
    string ans;
    for (long long int i = 0; i < n; i++) ans += '.';
    for (long long int i = 0; i < n; i++) {
      if (second[i] == '0') {
        if (i - x >= 0) ans[i - x] = '0';
        if (i + x < n) ans[i + x] = '0';
      }
    }
    long long int temp = 0;
    for (long long int i = 0; i < n; i++) {
      if (second[i] == '1') {
        if (i - x >= 0 && i + x < n && ans[i - x] == '0' && ans[i + x] == '0') {
          temp = 1;
          break;
        }
        if (i - x >= 0 && ans[i - x] == '0' && i + x >= n) {
          temp = 1;
          break;
        }
        if (i - x < 0 && i + x < n && ans[i + x] == '0') {
          temp = 1;
          break;
        };
        if (i - x < 0 && i + x >= n) {
          temp = 1;
          break;
        }
      }
    }
    if (temp == 1) {
      cout << "-1\n";
    } else {
      for (long long int i = 0; i < n; i++) {
        if (ans[i] == '.') ans[i] = '1';
      }
      cout << ans << "\n";
    }
  }
}
