#include <bits/stdc++.h>
using namespace std;
bool ok;
int n, x;
string s;
bool w[300000];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> s >> x;
    n = s.size();
    ok = true;
    for (int i = 0; i < n; i++) w[i] = 1;
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i + x < n) w[i + x] = 0;
        if (i - x >= 0) w[i - x] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        if (i + x < n && w[i + x]) continue;
        if (i - x >= 0 && w[i - x]) continue;
        ok = false;
        break;
      }
    }
    if (!ok)
      cout << "-1\n";
    else {
      for (int i = 0; i < n; i++) cout << w[i];
      cout << "\n";
    }
  }
}
