#include <bits/stdc++.h>
int fx[] = {0, 0, +1, -1};
int fy[] = {+1, -1, 0, 0};
using namespace std;
const int maX = 1e3 + 1;
vector<long long> Graph[maX], cost[maX];
long long visit[maX];
long long color[maX];
long long cc = 0;
void dfs(long long x, long long y, long long z) {
  visit[x] = 1;
  if (x == z) return;
  for (long long i = 0; i < Graph[x].size(); i++) {
    long long u = Graph[x][i];
    if (visit[u] == 0 && cost[x][i] == y) {
      cc++;
      dfs(u, y, z);
    }
  }
}
int main() {
  long long n, m, i, j, k = 0;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    long long x, y, z;
    cin >> x >> y >> z;
    Graph[x].push_back(y);
    Graph[y].push_back(x);
    cost[x].push_back(z);
    cost[y].push_back(z);
    color[z] = 1;
  }
  long long q;
  cin >> q;
  while (q--) {
    k = 0;
    long long x, y;
    cin >> x >> y;
    for (i = 1; i <= m; i++) {
      memset(visit, 0, sizeof(visit));
      if (color[i]) {
        dfs(x, i, y);
        if (visit[y]) {
          k += 1;
        }
      }
    }
    cout << k << endl;
  }
}
