#include <bits/stdc++.h>
using namespace std;
void print1() { cout << "1"; }
void print0() { cout << "0"; }
int main() {
  int t;
  cin >> t;
  while (t--) {
    string s;
    cin >> s;
    long long int x;
    cin >> x;
    vector<char> vec(s.length(), '2');
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '0') {
        if (i - x >= 0) vec[i - x] = '0';
        if (i + x < s.length()) vec[i + x] = '0';
      }
    }
    long long int a = 1;
    int b = 0, c = 0, d = b + c;
    for (int i = 0; i < s.length(); i++) {
      if (s[i] == '1') {
        if ((i - x >= 0 && vec[i - x] == '0') || i - x < 0) {
          if (i + x < s.length() && vec[i + x] == '0' || i + x >= s.length()) {
            a = 0;
            break;
          }
        }
      }
    }
    if (a == 0) {
      cout << -1 << endl;
      continue;
    }
    for (int i = 0; i < s.length(); i++) {
      if (vec[i] == '2')
        print1();
      else
        print0();
    }
    cout << endl;
  }
  return 0;
}
