#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  int t;
  cin >> t;
  while (t--) {
    string second;
    int x, n;
    cin >> second >> x;
    n = second.size();
    int ans[100010];
    for (int i = 0; i < n; i++) ans[i] = 1;
    for (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;
      }
    }
    int deu = 1;
    for (int i = 0; i < n; i++) {
      if (second[i] == '1') {
        if ((i - x < 0 || ans[i - x] == 0) && (i + x >= n || ans[i + x] == 0)) {
          deu = 0;
          break;
        }
      }
    }
    if (!deu)
      cout << -1 << endl;
    else {
      for (int i = 0; i < n; i++) cout << ans[i];
      cout << endl;
    }
  }
}
