#include <bits/stdc++.h>
using namespace std;
int t, x, sum, flag = 1;
char a[200000];
bool b[200000];
int main() {
  bool flag = false;
  cin >> t;
  while (t--) {
    flag = false;
    int j;
    cin >> a >> x;
    j = strlen(a);
    memset(b, true, sizeof(b));
    for (int i = 0; i < j; i++) {
      if (i - x >= 0 && a[i] == '0') b[i - x] = false;
      if (i + x < j && a[i] == '0') b[i + x] = false;
    }
    int y = 1;
    for (int i = 0; i < j; i++) {
      if (a[i] == '0') continue;
      flag = false;
      if ((b[i - x] == true && i - x >= 0) || (b[i + x] == true && i + x < j))
        flag = true;
      if (!flag) {
        y = 0;
        break;
      }
    }
    if (y == 1) {
      for (int i = 0; i < j; i++) {
        if (b[i] == true)
          cout << "1";
        else
          cout << "0";
      }
    } else
      cout << "-1";
    cout << endl;
  }
  return 0;
}
