#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  int test, cs = 1;
  cin >> test;
  while (test--) {
    string s, res;
    cin >> s;
    int x, i, j, n = (int)(s.size());
    cin >> x;
    res.resize(n, '1');
    for (i = 0; i < n; i++) {
      if (i + x < n && s[i + x] == '0') res[i] = '0';
      if (i - x >= 0 && s[i - x] == '0') res[i] = '0';
    }
    bool ok = true;
    for (i = 0; i <= n; i++) {
      if (s[i] == '1') {
        if ((i + x < n && res[i + x] == '1') ||
            (i - x >= 0 && res[i - x] == '1'))
          ok = true;
        else {
          ok = false;
          break;
        }
      }
    }
    if (ok)
      cout << res << endl;
    else
      cout << -1 << endl;
  }
}
