#include <bits/stdc++.h>
using namespace std;
void solve() {
  string s;
  cin >> s;
  long long int n = s.size();
  long long int x;
  cin >> x;
  long long int a[n + 1];
  for (long long int i = 0; i < n; i++) {
    a[i] = 1;
  }
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '0') {
      if (i - x >= 0) a[i - x] = 0;
      if (i + x < n) a[i + x] = 0;
    }
  }
  for (long long int i = 0; i < n; i++) {
    if (s[i] == '1') {
      bool ok = false;
      if (i + x < n) {
        if (a[i + x] == 1) ok = true;
      }
      if (i - x >= 0) {
        if (a[i - x] == 1) ok = true;
      }
      if (ok == false) {
        cout << "-1\n";
        return;
      }
    }
  }
  for (long long int i = 0; i < n; i++) {
    cout << a[i];
  }
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long int t = 1;
  cin >> t;
  long long int ctr = 0;
  while (t--) {
    ctr++;
    solve();
  }
}
