#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    int n = s.size();
    string a(n, '1');
    for (int i = 0; i < n; ++i) {
      if (s[i] == '1') continue;
      if (i - x >= 0) a[i - x] = '0';
      if (i + x < n) a[i + x] = '0';
    }
    string w;
    w = a;
    for (int i = 0; i < n; ++i) {
      if (i - x >= 0 && a[i - x] == '1') {
        w[i] = '1';
        continue;
      }
      if (i + x < n && a[i + x] == '1') {
        w[i] = '1';
        continue;
      }
      w[i] = '0';
    }
    if (w != s) {
      cout << -1 << '\n';
      continue;
    }
    cout << a << '\n';
  }
  return 0;
}
