#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    int x;
    cin >> x;
    int n = s.size();
    vector<char> w(s.size(), '1');
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '1') continue;
      if (i - x >= 0) w[i - x] = '0';
      if (i + x < n) w[i + x] = '0';
    }
    int flag = 0;
    for (int i = 0; i < s.size(); i++) {
      if (s[i] == '0') continue;
      if (i - x >= 0 && s[i] == w[i - x]) continue;
      if (i + x < n && s[i] == w[i + x]) continue;
      flag = 1;
    }
    if (flag)
      cout << -1;
    else {
      for (auto x : w) cout << x;
    }
    cout << endl;
  }
}
