#include <bits/stdc++.h>
using namespace std;
int main() {
  int a, x;
  scanf("%d", &a);
  while (a--) {
    string str;
    cin >> str;
    int wrong = 1, right, changdu = str.length();
    cin >> x;
    string str2(changdu, '1');
    for (int i = 0; i < changdu; i++) {
      if (str[i] == '0') {
        if (i - x >= 0) str2[i - x] = '0';
        if (i + x < changdu) str2[i + x] = '0';
      }
    }
    for (int i = 0; i < changdu; i++) {
      right = 0;
      if ((i - x) >= 0 && str2[i - x] == '1') right = 1;
      if ((i + x) < changdu && str2[i + x] == '1') right = 1;
      if (str[i] == '1') {
        if (right == 0) {
          wrong = 0;
          cout << -1;
          break;
        }
      }
    }
    if (wrong) cout << str2;
    cout << endl;
  }
}
